input - How can I improve this Swift readLine code? -


it works, don't think enough. possible use readline() outside if-let construct??? find scope limiting , not believe can pass arguments using command line using method. can suggest better approach issue?

    import foundation      print("\ntemperature conversion\n")     print("what current temperature unit?\n ")     print("valid options f or f fahrenheit, c or c celsius: \n")      if var  temp = readline() {         switch temp{         case "c","c":             print("and temperature: ")             if var  degrees = readline() {                 var fahr = (5 * float(degrees)! * 1.8 + 32)                 print("\(degrees) degrees equal \(fahr) degrees fahrenheit. \n")             } else {                 print("you entered invalid temperature")             }         case "f","f":             print("fahrenheit")             if var degrees = readline() {                 var celsius = (5 * float(degrees)! - 32) / 9;                 print("\(degrees) degrees equal \(celsius) degrees fahrenheit. \n")             } else {                 print("you entered invalid temperature")             }         default:             print("not valid temperature unit")         }     } 

readline() returns string?, nil if there no more input (eof ctrl-d in xcode). need unwrap somehow , if let statement perfect that.

arguments have nothing standard input (readline()), can arguments instead:

process.arguments // type [string] 

Comments

Popular posts from this blog

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

matlab - error with cyclic autocorrelation function -

php - Using grpc in Laravel, "Class 'Grpc\ChannelCredentials' not found." -