Predict the output
What does this print?
let input = "42a"
let n = Int(input) ?? -1
print(n)
let name: String? = nil
print(name?.count ?? 0)Hint 1 of 1
Does Int("42a") succeed?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: B -1 then 0
Correct. The failed parse falls back to -1, and the nil chain falls back to 0.
Why not the others?
- A 42 then 0.
Int("42a")fails and returns nil, because the whole string must be a number. - C -1 then nil.
?? 0replaces the nil from optional chaining. - D It crashes. Nothing is force-unwrapped.