swift input | Sololearn: Learn to code for FREE!


swift input

var a : String = readLine() // why this code causes error, why we must use optional like var a : String? = readLine()

9/13/2020 5:35:01 AM

David Yan

2 Answers

New Answer


Okay so the problem is that using it as : var a : String = readline() will produce an error because the readLine function gives an optional. Write it like this : var a : String = readLine()! The exclamation mark is important because it is used to forcefully unwrap the optional. That's wild also cause an error if no value is imputed, so he sure to put a value. https://code.sololearn.com/cDU569xvCnss/?ref=app


readLine() return “String?”(optional), so this is something about return type