+ 4

swift input

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

13th Sep 2020, 5:35 AM
David Yan
David Yan - avatar
2 Answers
+ 4
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
15th Sep 2020, 5:22 PM
🔥 Destiny 🔥
+ 1
readLine() return “String?”(optional), so this is something about return type
13th Sep 2020, 12:26 PM
David Yan
David Yan - avatar