This is my third post on the Swift command line. In the first, I wrote on how to do command line input with Swift. In the second, I cleaned up the code after a year of tinkering with Swift. In this third installment, I get rid of more Objective-C.
Almost a month ago, Apple released Swift into the open source community. Pretty spiffy that. So, being the language wonk that I am, I bopped over to the Swift site and downloaded the sources. After a bit of scrounging, I discovered that there existed a standard library call to read a line from the input stream. I was a bit disappointed that I’d not seen this talked about anywhere, but that’s life.
So, I’ve updated my code sample and present it below. I do some forced unwrapping as the point is to highlight the process. Be sure to code defensively.
I’ll probably come back to this one more time once I’ve had a chance to look more into the Swift standard library so I can eliminate my untidy Objective-C dependent putString() routine.
//
// swift_intput_routines.swift
//
// Created by Charles Wilson on 9/27/2014.
// Revised by Charles Wilson on 9/27/2015.
// Revised by Charles Wilson on 12/21/2015.
//
// Copyright (c) 2014 – 2015 Charles Wilson. All rights reserved.
//
// Permission is granted to use and modify so long as attribution is made.
//
import Foundation
func putString (outputString : String = “”)
{
if !outputString.isEmpty
{
NSFileHandle.fileHandleWithStandardOutput().writeData((outputString as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
}
}
func getString (prompt : String = “”) -> String?
{
if !prompt.isEmpty
{
putString(prompt)
}
return readLine(stripNewline: true)
}
func getCharacter () -> Character
{
let inputValue : UInt32 = UInt32(getchar())
var inputCharacter : Character
inputCharacter = Character(UnicodeScalar(inputValue))
return inputCharacter
}
func getInteger (prompt : String = “”) -> Int
{
if !prompt.isEmpty
{
putString(prompt)
}
return (getString()! as NSString).integerValue
}
func getFloat (prompt : String = “”) -> Float
{
if !prompt.isEmpty
{
putString(prompt)
}
return (getString()! as NSString).floatValue
}
Here’s the main to exercise it.
// // main.swift // // Created by Charles Wilson on 9/27/2014. // Revised by Charles Wilson on 9/27/2015. // Revised by Charles Wilson on 12/21/2015. // // Copyright (c) 2014 - 2015 Charles Wilson. All rights reserved. // // Permission is granted to use and modify so long as attribution is made. // import Foundation var name = getString("What is your name? ")! if name.isEmpty { name = "George" putString("That's not much of a name. I'll call you '\(name)'\n") } else { putString("Your name is '\(name)'\n") } let age = getInteger("How old are you \(name)? ") putString("You are \(age) years old\n") // get a value without a prompt let number = getInteger() putString("\(number) is a nice number\n") let floating = getFloat("Enter a floating point number: ") putString("\(floating) works for me\n") var c : Character putString("Type stuff. Enter ^ when done.\n") let sentinel : Character = Character(UnicodeScalar("^")) var in_c : Character repeat { in_c = getCharacter() } while ( in_c != sentinel ) putString("\n\n") putString("bye\n")
[…] Update: Apple released the Swift source. My update based on what I learned is here. […]
[…] 2015-12-21: Apple released Swift source code. I’ve updated my sample again to reflect what I […]