+ 5

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
Zhengrong Yan
Zhengrong Yan - avatar
2 ответов
+ 5
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
Zhengrong Yan
Zhengrong Yan - avatar