Chapter 5

Numbers

Numbers have their own data types in Swift. Like with other programming languages, in Swift some number data types can store larger numbers than others, and numbers that have a fractional component are treated differently than numbers that are whole numbers.

Integers

Integers are whole numbers that may be either positive or negative. Integers don’t have any decimal places. For example, 1, 2, and -9 are all integers. While there are several integer data types, you will usually use the Int data type. Int is used when you don’t need to specify a size for the integer. Listing 5-1 shows two examples of declaring integers.

Listing 5-1. Declaring Integers

let i1:Int = 5var i2 = 7

In Listing 5-1, i1 is an integer constant, while ...

Start Free Trial

No credit card required