Swift

Swift: How to input a number from CLI Command Line Interface

Is there no input() function in Swift? No. Not for now. But how then can we enter a number or a string from the command line interface (CLI)?

1. Copy this function to your code:

func input() -> String {
    var keyboard = NSFileHandle.fileHandleWithStandardInput()
    var inputData = keyboard.availableData
    var strData = NSString(data: inputData, encoding: NSUTF8StringEncoding)!
    return strData.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
}

2. Use it like so:

print("Enter number: ")
let n_str = input()       // NOTE: n_str is an Optional (type), this means String or nothing
let n = n_str.toInt()!    // NOTE: ! "unpacks" the Optional type n_str

 

3. Testing: What happens, if I enter a Number:

Enter number: 12

Result: Just seems to work

 

4. Testing: What happens if I just enter ENTER:

Enter number:        // <--- I just hit ENTER here
fatal error: unexpectedly found nil while unwrapping an Optional value
0  swift                    0x00000001039d82b8 llvm::sys::PrintStackTrace(__sFILE*) + 40
1  swift                    0x00000001039d8794 SignalHandler(int) + 452
[...]

Result: It crashes, which is better then continue with undefined values

5. Testing: What happens if I just enter a string not a number:

Enter Number: abc
fatal error: unexpectedly found nil while unwrapping an Optional value
0  swift                    0x000000010bc512b8 llvm::sys::PrintStackTrace(__sFILE*) + 40
1  swift                    0x000000010bc51794 SignalHandler(int) + 452
[...]

Result: It does crash, witch is better then continue with undefined values!

6. Conclusion

So, sometimes it's a better your program crashes then just go ahead somehow undefined as it's possible with plain C for example.

Missing Swift Computer Language Feature: But really, Swift should and has to include an input() function by default. Apple, if you read this, just do it 😉 Swift seems to be a great new computer language and I start to really like it.

 

M

Share
Published by
M

Recent Posts

Bibel Patch

Bibel Patch? Was ist das? Bibeln sind Übersetzungen und können auch Fehler enthalten. Nicht jede…

9 Jahren ago

SpeedreaderLTS – Speech Therapy Software for Logopedia

We write letters and words, but we pronounce syllables. With this method every child can…

9 Jahren ago

macOS Sierra – TOP 10 Features you MUST HAVE

macOS Sierra fixes also some tiny little silly bugs which is as good thing. TOP…

9 Jahren ago

Camino Alternative Dodo Browser Mac

Camino Alternative Dodo Browser Mac Click here https://www.flagsoft.com/cmswp/en/software/dodo-browser/ Camino, SeaMonkey, Maxthon Browser, Flock (web browser),…

10 Jahren ago

Real Time Mac Picture and Image Viewer for Event Photographers

Looking for Real Time Picture and Image Viewer for Event Photographers on your Mac? Click…

10 Jahren ago

PictureViewer with Real Time Viewing Event Images WiFi SD Card, eyefi

Real Time Event Photography - Real time pictures over WiFi SD Cards So you take…

10 Jahren ago