Swift for Open Source Developers

42 min read

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. In December 2015, Apple open sourced Swift at https://swift.org and made binaries available for Linux as well as OS X. The content in this article can be run on either Linux or OS X. Developing iOS applications requires Xcode and OS X.

In this article, we will present the following topics:

  • How to use the Swift REPL to evaluate Swift code
  • The different types of Swift literals
  • How to use arrays and dictionaries
  • Functions and the different types of function arguments
  • Compiling and running Swift from the command line

(For more resources related to this topic, see here.)

Open source Swift

Apple released Swift as an open source project in December 2015, hosted at https://github.com/apple/swift/ and related repositories. Information about the open source version of Swift is available from the https://swift.org site. The open-source version of Swift is similar from a runtime perspective on both Linux and OS X; however, the set of libraries available differ between the two platforms.

For example, the Objective-C runtime was not present in the initial release of Swift for Linux; as a result, several methods that are delegated to Objective-C implementations are not available. “hello”.hasPrefix(“he”) compiles and runs successfully on OS X and iOS but is a compile error in the first Swift release for Linux. In addition to missing functions, there is also a different set of modules (frameworks) between the two platforms. The base functionality on OS X and iOS is provided by the Darwin module, but on Linux, the base functionality is provided by the Glibc module. The Foundation module, which provides many of the data types that are outside of the base-collections library, is implemented in Objective-C on OS X and iOS, but on Linux, it is a clean-room reimplementation in Swift. As Swift on Linux evolves, more of this functionality will be filled in, but it is worth testing on both OS X and Linux specifically if cross platform functionality is required.

Finally, although the Swift language and core libraries have been open sourced, this does not apply to the iOS libraries or other functionality in Xcode. As a result, it is not possible to compile iOS or OS X applications from Linux, and building iOS applications and editing user interfaces is something that must be done in Xcode on OS X.

Getting started with Swift

Swift provides a runtime interpreter that executes statements and expressions. Swift is open source, and precompiled binaries can be downloaded from https://swift.org/download/ for both OS X and Linux platforms. Ports are in progress to other platforms and operating systems but are not supported by the Swift development team.

The Swift interpreter is called swift and on OS X can be launched using the xcrun command in a Terminal.app shell:

$ xcrun swift

Welcome to Swift version 2.2!  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 on the same machine without conflicting.

On Linux, the swift binary can be executed provided that it and the dependent libraries are in a suitable location.

The Swift prompt displays > for new statements and . for a continuation. Statements and expressions that are typed into the interpreter are evaluated and displayed. Anonymous values are given references so that 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 literals

Numeric types in Swift can represent both signed and unsigned integral values with sizes of 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. Please 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

 

Floating point literals

There are three floating point types that are available in Swift which use the IEEE754 floating point standard. The Double type represents 64 bits worth of data, while Float stores 32 bits of data. In addition, Float80 is a specialized type that stores 80 bits worth of data (Float32 and Float64 are available as aliases for Float and Double, respectively, although they are not commonly used in Swift programs).

Some CPUs internally use 80 bit precision to perform math operations, and the Float80 type allows this accuracy to be used in Swift. Not all architectures support Float80 natively, so this should be used sparingly.

By default, floating point values in Swift use 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. For example:

> 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

String literals

Strings can contain escaped characters, Unicode characters, and interpolated expressions. Escaped characters start with a slash () and can be one of the following:

  • \: This is a literal slash
Packt

Share
Published by
Packt

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago