User input code using Swift 3

Continuing the discussion from Learn to Code iOS Apps with Swift Tutorial 2: Your First Project:


I’ve downloaded Xcode 8 beta 6 trying to learn Swift 3.

I was trying to use your Swift beginner code but need some help with the Swift 3 changes
for the guessing game that takes keyboard input or user input. The current code doesn’t work in Swift 3.
I realized that I’m running a beta version of Xcode and that any code you give me now may change again
but that’s ok. I just want to get it working at this time.

Here’s the current code that I’m talking about:

func input() → String {
let keyboard = NSFileHandle.fileHandleWithStandardInput()
let inputData = keyboard.availableData
let rawString = NSString(data: inputData, encoding:NSUTF8StringEncoding)
if let string = rawString {
return string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
} else {
return “Invalid input”

Thanks for your help in advance!

What seems to be the problem? What errors does xcode give you?

I haven’t run the code in the latest version of Xcode, but I wouldn’t be surprised if it was broken. We’ll be updating the tutorial pretty soon.

Thanks for your reply.
Xcode 8 should be released soon. I’m looking forward to the tutorial updates but will work
with learning Swift 2.2 for now.


Sorry, I couldn’t tell you now. There were several error messages. In the interim, I’ve backed out of
Xcode 8 Beta 6 using Swift 3 and have dropped back to Swift 2.2 where all the tutorial code is
working fine.

I assume Apple will be releasing Xcode 8 either this next Wed the 7th or sometime later this month.
I’ll wait until the final version of Xcode 8 is released before I start using Swift 3 , again.

I know all the tutorials will be updated to Swift 3 sometime soon so I won’t encounter those
previous problems that I was having using Xcode 8 Beta 6.

Thanks for your response!


OK. Next time feel free to throw up all errors and we can help you sort them out one by one.

Thanks Marcio. I appreciate it!