Programming Languages Swift Interview
Oct 01, 2025

Explain optionals in Swift with examples.

Detailed Explanation
Optionals represent values that might be absent. Declaration: var name: String? = nil. Unwrapping methods: 1) Force unwrapping: name! (crashes if nil), 2) Optional binding: if let safeName = name { }, 3) Nil coalescing: name ?? "Default", 4) Optional chaining: person?.address?.street. Optionals prevent runtime crashes by making nil handling explicit and safe.
Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback