TExcelTAD
Home › Swift › Swift Data Types

Swift Data Types

Every value in Swift has a type that decides what you can do with it. You can add two Ints, uppercase a String and negate a Bool, but you cannot multiply a String by a Bool.

The basic types

Type Holds Example
Int whole numbers (64-bit on modern devices) 42, -7
Double decimal numbers, ~15 digits precision 3.14159
Float decimal numbers, ~6 digits precision 2.5
Bool true or false true
String text "Hello"
Character a single character "A", "🙂"
swift · try it yourself
let age: Int = 29
let height: Double = 1.74
let ratio: Float = 0.5
let isMember: Bool = true
let name: String = "Lena"
let initial: Character = "L"

print(age, height, ratio, isMember, name, initial)
print(type(of: age), type(of: height), type(of: initial))
29 1.74 0.5 true Lena L
Int Double Character

type(of:) tells you the type of any value at runtime — handy while learning.

Integers

Int is the integer type you should use by default. Swift also has sized variants (Int8, Int16, Int32, Int64) and unsigned ones (UInt, UInt8, …) for special cases such as binary file formats. Each has a min and max:

swift · try it yourself
print("Int8:  \(Int8.min) ... \(Int8.max)")
print("UInt8: \(UInt8.min) ... \(UInt8.max)")
print("Int:   \(Int.min) ... \(Int.max)")

let population = 8_100_000_000   // underscores make big numbers readable
print(population)
Int8:  -128 ... 127
UInt8: 0 ... 255
Int:   -9223372036854775808 ... 9223372036854775807
8100000000

Integer literals can also be written in other bases: 0b1010 (binary), 0o12 (octal) and 0xA (hexadecimal) are all the number 10.

Note: Swift checks for overflow. Int8.max + 1 does not silently wrap to -128 — the program stops with an error. Use the overflow operators &+, &- and &* when you really want wrapping.

Floating-point numbers

Use Double unless you have a reason not to; a decimal literal is a Double by default. Like in every language, floating-point numbers are binary approximations:

swift · try it yourself
let a = 0.1 + 0.2
print(a)
print(a == 0.3)
print(Double.pi)
let f: Float = 1.0 / 3.0
let d: Double = 1.0 / 3.0
print(f, d)
0.30000000000000004
false
3.141592653589793
0.33333334 0.3333333333333333

Booleans, strings and characters

Bool has exactly two values, true and false. String is a sequence of Character values; one Character can be a letter, a digit, an emoji or even a flag made of several Unicode scalars.

swift · try it yourself
let word = "Swift🚀"
print(word.count)
print(word.first!)
print(word.last!)
print(word.uppercased())
print(word.isEmpty)
6
S
🚀
SWIFT🚀
false

Collections at a glance

You will learn these in their own chapters, but here is a preview:

Type Description Literal
[Int] (Array) ordered list [1, 2, 3]
[String: Int] (Dictionary) key–value pairs ["a": 1, "b": 2]
Set<String> unique, unordered values ["x", "y"] with a type annotation
(Int, String) (Tuple) a fixed group of values (404, "Not Found")

Type safety

Swift never converts between types silently. An Int and a Double cannot even be added together without a conversion — the next chapter shows how.

Test yourself

What is the type of let x = 7.0?

  1. Int
  2. Float
  3. Double
  4. Number

What is UInt8.max?

  1. 127
  2. 128
  3. 255
  4. 65535

What does "Hi👋".count return?

  1. 2
  2. 3
  3. 4
  4. 6