Swift For Dummies
Book image
Explore Book Buy On Amazon

As with other languages, Swift provides developers a common library of utility code that’s frequently used in development. A library like this is separate from the language itself, although it uses the language’s syntax and features. Although you can replace this library with another, non-standard one, most people prefer the standard library, and, in fact, replacements are very rare.

The contents of a standard library reflect the language and its intended uses. For example, the C standard library (sometimes called the ISO C Library) contains routines for string handling, mathematical functions, input/output, and character manipulation. The library represents nearly 300 pages of the entire 650-page C language specification.

By contrast, the Swift standard library is 50 pages long at this time; it contains:

  • types, which include:

    • strings

    • arrays

    • dictionaries

    • numeric types

  • protocols

  • free functions

If you want to delve deeply into the C library as well as Swift, you’ll find that some of the functionality described in the C library is implemented directly in Swift as well as in the Cocoa and Cocoa Touch frameworks. What matters at this point is that the standard library is the ultimate reference for Swift types.

Strings

A Swift string is an ordered set of characters. When you use a string literal, you enclose the characters in quotes like this.

"ABCDE"

The Objective-C syntax is not used so you can forget the @:

@"ABCDE"

Swift begins from the premise that its users will be international, so any Unicode character is acceptable as a string value or for the name of a ­constant or string.

Arrays

Swift arrays are ­similar to arrays in other languages except that all elements of an array must be of the same type, and the array is then said to be of that type. In the case of classes, as opposed to values (enumerations and structures), the common type for elements of an array may be a superclass.

Thus, an array of UIView instances can still be a UIView array if it contains UILabel and UIButton instances, which are both subclasses of UIView.

Unlike arrays in Objective-C, Swift arrays are not classes; they are actually implemented as structures.

Dictionaries

Like arrays, dictionaries have a common type for their elements. In other languages, dictionaries are called associative arrays. Each element has a key value rather than a number index. The key values are converted to index values that can locate a dictionary value.

Numeric types

The Swift standard library provides support for common numeric types. The basic types are:

  • Boolean: The Bool type has values true and false. (Note that this ­differs from Objective-C, which supports YES and NO as well.)

  • Integer: The Int type is a full word interpreted as a single binary integer. The size of words changes from time to time (at the moment, both 32- and 64-bit words are common, depending on the device). Because of this variation and the likely changes in the future, make no assumptions about word size. Also additional types exist for signed and unsigned integers, as shown in the table.

  • Floating point: Double is the most commonly used floating-point type. It uses 64 bits — but a 32-bit Float is also available.

Swift Integer Types
Length (bits) Signed Unsigned
8 Int8 UInt8
16 Int16 UInt16
32 Int32 Uint32
64 Int64 Uint64

About This Article

This article is from the book:

About the book author:

Jesse Feiler is a developer, consultant, and author specializing in Apple technologies. He is the creator of Minutes Machine for iPad, the meeting management app, and Saranac River Trail and is heard regularly on WAMC Public Radio for the Northeast’s The Roundtable.

This article can be found in the category: