Swift string input for OS X (updated)

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 http://swiftdoc.org/v2.2/func/readLine/

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?
Ronny
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 http://soltveit.org/xcode-swift-string-input-in-command-line/

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!

About Author

Related Posts

Xcode 9 uiimage image not shown

Xcode 9 uiimage image not shown

Xcode 9 uiimage image not shown Working on some ios apps created with Xcode 8 and your images don’t show after upgrading to Xcode 9? Took me awhile…

Swift Capitalize

Swift Capitalize words, How to

Swift Capitalize, How to Swift Capitalize is done with a string instance property. Check the example below to see how to use it. There is also a link…

Swift Debian

Swift Debian 8 (Jessie)

Swift Debian 8 (Jessie) Swift, Apples new programming language, to take over for Objective C has been open source for some time now. It only took at…

didMove never get called

Xcode – didMove never get called

didMove never get called in Xcode? didMove never get called when creating a new project with Xcode using the Game template. I selected Swift and SpriteKit. I…

Swift Change UIButton text

Swift Change UIButton text

Swift Change UIButton text How to in Swift change UIButton text?A common mistake, also done by myself some time ago, is to try to change it through…

swift play mp3

Swift play mp3 from your app

Swift play mp3 from your app Swift play mp3 from your app with this small example. To make swift play mp3 files we need to start with…

Leave a Reply