Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Swift Basics  ›  Lesson

Type Annotations and Type Inference

Type Annotations 12 minSwift infers each value's type from its first value, and a type annotation states the type explicitly when there is no value yet or the inferred type is wrong.

Type Annotations and Type Inference

Sprout's daily goal was let dailyGoal = 2, meaning two hours. Then someone asked for "one and a half hours" and dailyGoal = 1.5 would not compile. The value had been given a type without anyone writing it down. Knowing how Swift picks types, and when to state one yourself, stops these surprises.

The rule

Swift guesses the type from the value; annotate when the guess is wrong.

Every constant and variable has exactly one type, fixed forever. Swift can work it out from the first value, which is type inference:

let habitCount = 3          // Int
let dailyGoal = 1.5         // Double
let habitName = "Read"      // String
let isDoneToday = false     // Bool

A type annotation states the type yourself, with a colon after the name:

let dailyGoal: Double = 2   // 2 becomes 2.0, a Double
var reminderNote: String    // no value yet, so the type is required
var morning, evening: Int   // both are Int

Read the colon as "of type": dailyGoal, of type Double, equals 2.

You need an annotation in two situations:

  1. No starting value. Swift has nothing to infer from.
  2. The inferred type is not the one you want. A whole-number literal like 2 becomes Int; a literal with a decimal point becomes Double. If you want a Double from 2, or a Float at all, say so.

Otherwise, leave it out. Swift code is usually lighter without annotations, and the type is still just as fixed.

The shortcut

Look at the first value and read off its type: no dot means Int, a dot means Double, quotes mean String, true/false means Bool. If that answer is not what the value will need to hold later, add an annotation.

Fixing Sprout's daily goalStep 1 of 4
  1. Read the first value

    let dailyGoal = 2 has no decimal point, so Swift infers Int.

Annotations do not make Swift "more typed". Inferred or written, a type never changes after the declaration.

Drills

Answer fast — trust the rule.

quick drill

What type does Swift infer for let minutes = 25?

quick drill

Which declaration MUST have a type annotation?

quick drill

After var streak = 4, what happens with streak = "five"?

quick drill

In var morning, evening: Double, what type is morning?

Remember

  • Swift guesses the type from the value; annotate when the guess is wrong.
  • No dot is Int, a dot is Double, quotes are String, true/false is Bool.
  • Inferred or annotated, a type never changes after the declaration.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy the goal would not change

Explain why var dailyGoal = 2 followed by dailyGoal = 1.5 fails to compile, and show two different ways to fix the declaration.