Greetings fellow developers. I just started developing iOS applications as of a few days ago. I come from having a Java/C++ background. My main question for you is how I would go about fixing this code to accept double input. It just seems like a huge mess in regards to Swift. So, here is the code I created to accept input based on some pre-compiled function called input()
func input() -> String {
let keyboard = NSFileHandle.fileHandleWithStandardInput()
let inputData = keyboard.availableData
return NSString(data: inputData, encoding:NSUTF8StringEncoding) as! String
}As you can see the code here, accepts input from a String. So, I called this function in order to get user Input later on in the program. I tried and attempted to case the String input and make it a Double.
//Accept User input for cake length
print("Please enter the length of the cake (in): ")
var userInput = input()
cakeLength = Double(userInput)!Unfortunately, I'm getting a run-time error. In Xcode, It made me unwrap the "cakeLength = Double(userInput)" why is that? I'm just confused about this whole unwrapping. Also, why am I getting a run-time error when trying to get the user Input for Cake Length. I just want to know what I'm doing wrong so that I can use it as a learning experience. Thanks ladies and gents in advance.
Best regards!