Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Swift Essentials (Second Edition)
  • Table Of Contents Toc
Swift Essentials (Second Edition)

Swift Essentials (Second Edition) - Second Edition

By : Alex Blewitt
5 (3)
close
close
Swift Essentials (Second Edition)

Swift Essentials (Second Edition)

5 (3)
By: Alex Blewitt

Overview of this book

Swift was considered one of the biggest innovations last year, and certainly with Swift 2 announced at WWDC in 2015, this segment of the developer space will continue to be hot and dominating. This is a fast-paced guide to provide an overview of Swift programming and then walks you through in detail how to write iOS applications. Progress through chapters on custom views, networking, parsing and build a complete application as a Git repository, all by using Swift as the core language
Table of Contents (11 chapters)
close
close
10
Index

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 compilation step. Alternatively, Swift scripts can be compiled to a native executable that can be run without the overhead of the interpreter.

Interpreted Swift scripts

Save the following as hello.swift:

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

Tip

In Linux, the first line should point to the location of the swift executable, such as #!/usr/bin/swift.

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 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 that are passed from the command line start from one (1).

The program can be terminated using the exit function; however, this is defined in the operating system libraries 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 that are 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.

Note

Not all foundation libraries are implemented for Linux, which results in some differences of behavior. In addition, the underlying module for the base functionality is Darwin on iOS and OS X, and is Glibc on Linux. These can also be accessed with import Foundation, which will include the appropriate operating system module.

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

#!/usr/bin/env xcrun swift
import func Darwin.exit
# import func Glibc.exit # for Linux
let args = Process.arguments[1..<Process.arguments.count]
for arg in args {
  print("\(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 example, 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 compiler 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 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.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Swift Essentials (Second Edition)
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon