What a data type is
Every value in a Swift program has a type, which says what kind of data it is and what you can do with it. The five types you meet first are:
| Type | Holds | Example |
|---|---|---|
Int | whole numbers | 3, -12 |
Double | numbers with a fractional part | 3.14 |
String | text, a sequence of characters | "John Doe" |
Bool | true or false | true |
Character | one single character | "a", "?" |
A Character is written with double quotes like a string. Swift treats a literal as a String unless you ask for a Character with an annotation: let grade: Character = "A".
Why types matter
The same symbol can mean different things for different types. In print(a + b), the + adds when a and b are numbers and joins text when they are strings:
var a = 10
var b = -5
print(a + b)
var c = "Hello "
var d = "world!"
print(c + d)
5
Hello world!
Swift checks the types while it compiles the code. When an operation makes no sense for a type, it refuses to build the program. Two strings cannot be multiplied:
var a = "peanut"
var b = "butter"
print(a * b)
error: binary operator '*' cannot be applied to two 'String' operands
The error shows up before anything runs, which is why Swift is described as type-safe.
Declaring a type
You can state a type explicitly with an annotation, a colon and the type name after the constant's name:
let score: Int = 5
You can also leave it out and let Swift infer the type from the value. A whole-number literal becomes an Int, a literal with a decimal point becomes a Double, a quoted literal becomes a String and true or false becomes a Bool. An annotation can override the inference: let ratio: Double = 7 stores 7.0.
Where each type shows up
A Bool is usually the result of a comparison, and later it decides which code runs. With let age = 20, the line let isAdult = age >= 18 gives isAdult the value true and the type Bool.
A Double stores approximations of decimal numbers, so small rounding differences appear: 0.1 + 0.2 prints 0.30000000000000004. An Int is exact, which makes it the right choice for counts and ages.
A String knows how many characters it holds: "swift".count is 5. A number has no count.
The exercise, step by step
The lesson asks for four constants, one per type, each on its own line and in this order. Each step keeps the earlier ones.
myIntwith typeInt, set to a whole number.myFloatwith typeDouble, set to a value with a decimal point. The name says float, but the type to use in Swift isDouble.myStringwith typeString, set to some text in double quotes.myBoolwith typeBool, set totrueorfalse.
The solution in Swift
let myInt: Int = 5
let myFloat: Double = 3.14
let myString: String = "Hello"
let myBool: Bool = true
The details a learner should know:
IntandDoublenever mix silently. Withlet a = 10andlet b = 2.5, the expressiona * bis a compile error, because one operand is anIntand the other aDouble. Convert one side:Double(a) * bis25.0.- Integer division truncates.
7 / 2is3, while7.0 / 2is3.5. Intis 64 bits on 64-bit platforms, so it runs from -9223372036854775808 to 9223372036854775807.- The type is fixed. A
varholding anIntcannot later hold a string. - Use
type(of:)to check.type(of: 3.14)printsDouble.
Common mistakes
- Quotes around a number.
let myInt: Int = "5"does not compile, as the strings-versus-numbers lesson shows. - Capital letters in
trueandfalse. Swift's booleans are lowercase.Trueis an unknown name. - Single quotes for text. Swift strings and characters use double quotes.
Key takeaways
- The core types are
Int,Double,String,BoolandCharacter. - Swift checks types at compile time and rejects operations that do not fit.
- Annotate with
name: Type, or let Swift infer the type from the value.
