mardi 26 juillet 2016

Accept Integer Input in Swift

Can someone please tell me how to read a set of integers from the keyboard for a console application using Swift?

I have tried the following method:

func input() -> String {
    let keyboard = NSFileHandle.fileHandleWithStandardInput()
    let inputData = keyboard.availableData
    return (NSString(data: inputData, encoding: NSUTF8StringEncoding) as! String)
}

But this function will treat the entered value as a string. And converting the string to Int results in a nil value.

Is there is anything similar to scanf() or cin() like in C, C++ ?

Aucun commentaire:

Enregistrer un commentaire