iOS ● ○ ○ Swift 6

Kodebits Day 6: Guard Optionals

Apr 15 2026
Practice unwrapping optionals safely with guard.

What does this print?

func double(_ n: Int?) -> Int {
  guard let n else { return -1 }
  return n * 2
}
print(double(7))
print(double(nil))


Try it in the online Swift Playground →

[spoiler title="Solution"]

Answer:

14
-1

Explanation:

guard exits early when the value is nil. The first call doubles 7, the second returns -1.

[/spoiler]


Further Reading