2016-07-04 2 views
-1

효과가 있지만 충분하지 않다고 생각합니다. if-let 구조 외부에서 readLine()을 사용할 수 있습니까 ??? 나는 그 범위를 매우 제한적으로 생각하며,이 방법을 사용하여 커맨드 라인에서 이것을 사용하여 인자를 전달할 수 있다고 믿지 않습니다. 누구든지이 문제에 대한 더 나은 접근법을 제안 할 수 있습니까?이 신속한 readLine 코드를 개선하려면 어떻게해야합니까?

import Foundation 

    print("\nTemperature Conversion\n") 
    print("What is your current Temperature Unit?\n ") 
    print("Valid options are f or F for Fahrenheit, c or C for Celsius: \n") 

    if var temp = readLine() { 
     switch temp{ 
     case "c","C": 
      print("And the temperature: ") 
      if var degrees = readLine() { 
       var fahr = (5 * Float(degrees)! * 1.8 + 32) 
       print("\(degrees) Degrees is equal to \(fahr) degrees Fahrenheit. \n") 
      } else { 
       print("You entered an invalid temperature") 
      } 
     case "f","F": 
      print("Fahrenheit") 
      if var degrees = readLine() { 
       var celsius = (5 * Float(degrees)! - 32)/9; 
       print("\(degrees) Degrees is equal to \(celsius) degrees Fahrenheit. \n") 
      } else { 
       print("You entered an invalid temperature") 
      } 
     default: 
      print("Not a valid Temperature unit") 
     } 
    } 

답변

0

readLine()는 (엑스 코드 CTRL-D이다 EOF) 입력 없으면 nilString?를 반환한다. 어떻게 든 풀어야하고 if let 진술이 완벽합니다.

인수,이 대신 같은 인수를 얻을 수있는 표준 입력 (readLine()을)과는 아무 상관이 없다 :

Process.arguments // type [String] 
관련 문제