Swift course | Sololearn: Learn to code for FREE!


Swift course

So is their just no standard user input for Swift 4?

5/30/2021 6:30:39 AM

Roderick Davis

4 Answers

New Answer


Use readLine() to get the input as a String Optional (String?). You can then unwrap and convert the string to the desired type.


visph FYI, in the particular case of using readLine()! with the stdin it actually won't result in an error, because it will be equal to an empty String "", but in other cases it may, so better to safeguard your code appropriately. var s: String = readLine()! print(s=="") outputs true when you don't enter any value.


ChaoticDawg I googled and saw that and didn’t know how to “unwrap” the string


var s : String = readLine()! the exclamation mark make it 'unwrap' the string... that also cause an error if no input is provided ^^