Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Egor Bedunkevich
Egor Bedunkevich
643 Points

Is there a way to ask for user input in Swift?

When creating a console application, can I somehow ask for user input?

1 Answer

Dustin Morgan
Dustin Morgan
7,551 Points

Not easily but yes you can do it.

Import Foundation

func input() -> String {
    var keyboard = NSFileHandle.fileHandleWithStandardInput()
    var inputData = keyboard.availableData
    return NSString(data: inputData, encoding: NSUTF8StringEncoding) as! String
}
println("Please enter your input..")
var theInput = input()
println("\(theInput)")
Dairon Castellanos
Dairon Castellanos
1,043 Points

Is there any easy way to do this??? , swift is usually a really easy programing language , thats seems bit complicated .