Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Swift Essentials

You're reading from   Swift Essentials Get up and running lightning fast with this practical guide to building applications with Swift

Arrow left icon
Product type Paperback
Published in Dec 2014
Publisher
ISBN-13 9781784396701
Length 228 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Bandlem Limited Bandlem Limited
Author Profile Icon Bandlem Limited
Bandlem Limited
Alex Blewitt Alex Blewitt
Author Profile Icon Alex Blewitt
Alex Blewitt
Arrow right icon
View More author details
Toc

Command-line Swift

As Swift can be interpreted, it is possible to use it in shell scripts. By setting the interpreter to swift with a hashbang, the script can be executed without requiring a separate a compilation step. Alternatively, Swift scripts can be compiled to a native executable that can be run without the overhead of an interpreter.

Interpreted Swift scripts

Save the following as hello.swift:

#!/usr/bin/env xcrun swift
println("Hello World")

After saving, make the file executable by running chmod a+x hello.swift. The program can then be run by typing ./hello.swift, and the traditional greeting will be seen:

Hello World

Arguments can be passed in from the command line and interrogated in the process using the Process class through the arguments constant. As with other Unix commands, the first element (0) is the name of the process executable; the arguments passed on in the command line start from one (1).

The program can be terminated using the exit function; however, this is defined in the Foundation framework and so it needs to be imported in order to call this function. Modules in Swift correspond to Frameworks in Objective-C and give access to all functions defined as public API in the module. The syntax to import all elements from a module is import module, although it's also possible to import a single function using import func module.functionName.

A Swift program to print arguments in uppercase can be implemented as follows:

#!/usr/bin/env xcrun swift
import Foundation
let args = Process.arguments[1..<countElements(Process.arguments)]
for arg in args {
  println("\(arg.uppercaseString)")
}
exit(0)

Running this with hello world results in the following:

$ ./upper.swift hello world
HELLO
WORLD

Conventionally, the entry point to Swift programs is via a script called main.swift. If starting a Swift-based command-line application project in Xcode, a main.swift file will be created automatically. Scripts do not need to have a .swift extension. For instance, the previous example could be called upper and it would still work.

Compiled Swift scripts

While interpreted Swift scripts are useful for experimenting and writing, each time the script is started, it is interpreted using the Swift command-line tool and then executed. For simple scripts (such as converting arguments to upper case), this can be a large proportion of the script's execution time.

To compile a Swift script into a native executable, use the swiftc command with the -o output flag to specify a file to write to. This will then generate an executable that does exactly the same as the interpreted script, only much faster. The time command can be used to compare the running time of the interpreted and compiled versions:

$ time ./upper.swift hello world # Interpreted
HELLO
WORLD
real  0m0.145s
$ xcrun swiftc -o upper upper.swift     # Compile step
$ time ./upper hello world       # Compiled
HELLO
WORLD
real  0m0.012s

Of course, the numbers will vary and the initial step only happens once, but startup is very lightweight in Swift. The numbers mentioned earlier are not meant to be taken in magnitude but rather as relative to each other.

The compile step can also be used to link together many individual Swift files into one executable, which helps create a more organized project; Xcode will encourage having multiple Swift files as well.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image