August 2, 2021


Swift string input for OS X (updated)

Swift string input

Swift string input

Swift string input has become a lot easier than since I posted an article about it. In my previous article Swift string input required you to create your own function to read a input line. Then one viewer commented on my YouTube video a different method for Linux users. His method had a readLine function. When I tested this with Xcode in OS X it worked just fine. And I’m almost sure it wasn’t that easy about a year ago.

Here is the function we need to use

I tried to test it with Playground too, but I could not get it to work there.

Swift string input example

import Foundation

print("What is your name? ")
if let yourName = readLine() {
print("Hello, \(yourName)")

The output will be something like this.
What is your name?
Hello, Ronny

If this is all new to you, and want to have a look at the old method. Just for the fun of it. You will find that in this article

If you want to see a video on how to do it, you can see it below. I have updated my old video. It is still out there, but go for the updated version.

Thats how you do String input with Swift for OS X now (and Linux).

Happy coding!