Apple announced Swift at WWDC 2014 as a new programming language that combines experience with the Objective-C platform and advances in dynamic and statically typed languages over the last few decades. Before Swift, most code written for iOS and OS X applications was in Objective-C, a set of object-oriented extensions to the C programming language. Swift aims to build upon patterns and frameworks of Objective-C but with a more modern runtime and automatic memory management.
This chapter will present the following topics:
Swift provides a runtime interpreter that executes statements and expressions. The Swift interpreter is called swift and can be launched from the Xcode 6 tools using the xcrun command in a Terminal.app
shell:
$ xcrun swift Welcome to Swift! Type :help for assistance. >
The xcrun
command allows a toolchain command to be executed; in this case, it finds /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
. The swift
command sits alongside other compilation tools such as clang
and ld
, and permits multiple versions of the commands and libraries to be installed on the same machine without conflicting. The Swift prompt displays >
for new statements and .
for continuation. Statements and expressions typed into the interpreter are evaluated and immediately displayed. Anonymous values are given references, so they can be used subsequently:
> "Hello " + . "World" $R0: String = "Hello World" > 3 + 4 $R1: Int = 7 > $R0 $R2: String = "Hello World" > $R1 $R3: Int = 7
Numeric types in Swift can represent both signed and unsigned integral values with sizes 8, 16, 32, or 64 bits, as well as signed 32 or 64 bit floating point values. Numbers can include underscores to provide better readability; so 68_040
is the same as 68040
:
> 3.141 $R0: Double = 3.141 > 299_792_458 $R1: Int = 299792458 > -1 $R2: Int = -1 > 1_800_123456 $R3: Int = 1800123456
Numbers can also be written in binary, octal, or hexadecimal using prefixes 0b
, 0o
(zero and the letter "o") or 0x
. Note that Swift does not inherit C's use of a leading zero (0
) to represent an octal value, unlike Java and JavaScript which do. Examples include:
> 0b1010011 $R0: Int = 83 > 0o123 $R1: Int = 83 > 0123 $R2: Int = 123 > 0x7b $R3: Int = 123
There are three types of floating point values available in Swift, which use the IEEE754 floating point standard. The Double
type represents 64 bits worth of data whilst Float
stores 32 bits of data. In addition, Float80
is a specialized type that stores 80 bits worth of data.
By default, floating point values in Swift have the Double
type. As floating point representation cannot represent some numbers exactly, some values will be displayed with a rounding error; for example:
> 3.141 $R0: Double = 3.141 > Float(3.141) $R1: Float = 3.1400003
Floating point values can be specified in decimal or hexadecimal. Decimal floating point uses e
as the exponent for base 10, whereas hexadecimal floating point uses p
as the exponent for base 2. A value of AeB
has the value A*10^B
and a value of 0xApB
has the value A*2^B
:
> 299.792458e6 $R0: Double = 299792458 > 299.792_458_e6 $R1: Double = 299792458 > 0x1p8 $R2: Double = 256 > 0x1p10 $R3: Double = 1024 > 0x4p10 $R4: Double = 4096 > 1e-1 $R5: Double = 0.10000000000000001 > 1e-2 $R6: Double = 0.01 > 0x1p-1 $R7: Double = 0.5 > 0x1p-2 $R8: Double = 0.25 > 0xAp-1 $R9: Double = 5