Question about some code I wrote in regards to unwrapping

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!

This is two questions.


1. What run-time error?


2. A string that is supposed to contain a numeric representation may of course contain garbage. If the Double constructor cannot interpret the string, it returns nil, so the result is of type Double? rather than Double. Throwing ! on the end isn't a good solution, because if the result is nil, you have an error that must be handled (invalid input).

Question about some code I wrote in regards to unwrapping
 
 
Q