Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Swift's Data Types  ›  Lesson

Type Safety and Type Casting

Type Casting 12 minSwift never converts between types implicitly, so you convert by wrapping a value in the target type, and converting text to a number returns an optional.

Type Safety and Type Casting

Sprout adds reading minutes (a Double) to a count of bonus minutes (an Int) and the code will not compile. Coming from a language that converts quietly, this feels strict. It is the whole point: Swift makes every conversion visible, so none of them happens by accident.

The rule

Swift never converts for you: wrap the value in the type you want.

Type safety means a value of one type cannot be used where another is expected. Even between two number types:

let bonus = 5                 // Int
let minutesRead = 12.5        // Double
let total = minutesRead + bonus            // error
let fixed = minutesRead + Double(bonus)    // 17.5

To convert, call the target type with the value. This is how numbers change type:

Double(5)          // 5.0
Int(12.9)          // 12   (the fraction is cut off, not rounded)
String(4)          // "4"
Int("42")          // 42, but as an optional: Int?
Int("forty")       // nil

Turning text into a number can fail, so Int("...") gives back an optional, a value that might be missing. The next lesson shows how to open it safely.

One exception: plain literals have no fixed type until they are used, so let t = 12.5 + 5 is fine and gives 17.5. Only named values of different types refuse to mix.

For values whose exact type is not known in advance, Swift has casting operators: is checks a type, as? tries to treat a value as a type and gives nil if it cannot.

let item: Any = 3
item is Int              // true
item as? String          // nil

The shortcut

At every place two values meet, ask "same type?" If not, pick the type the answer should have and wrap the odd one out: Double(x), Int(x), String(x). For Int(text), expect an optional back.

Showing total minutes in SproutStep 1 of 4
  1. The values

    minutesRead is Double 12.5. bonus is Int 5.

The trap is expecting Int(12.9) to round. It cuts off the fraction and gives 12. Use .rounded() first when you want 13.

Drills

Answer fast — trust the rule.

quick drill

With let a = 5 and let b = 2.5, which line compiles?

quick drill

What is Int(9.99)?

quick drill

What type is Int("12")?

quick drill

Why does let t = 12.5 + 5 compile when adding a Double variable to an Int variable does not?

Remember

  • Swift never converts for you: wrap the value in the type you want.
  • At every meeting point, ask "same type?" and wrap the odd one out.
  • Int(x) cuts off fractions; Int(text) returns an optional.

Practice — level up

0/1 solved

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

ExplainMake the types meet

Sprout has let minutesRead = 12.5 and let bonus = 5, plus let typed = "15" from a text field. Explain how to compute the total of all three as a Double, and why Swift makes you do each conversion yourself.