Recall & Review
beginner
What does
nil represent in Swift?nil means there is no value or the value is missing.
Click to reveal answer
beginner
How do you declare an optional variable that can hold
nil?Use a question mark ? after the type, like var name: String?.
Click to reveal answer
intermediate
What happens if you try to use a variable that is
nil without unwrapping it?Swift will give a compile error because you must safely check or unwrap optionals before use.
Click to reveal answer
intermediate
How can you safely unwrap an optional that might be
nil?You can use if let or guard let to check and unwrap the value safely.
Click to reveal answer
beginner
What is the difference between
nil and an empty string ""?nil means no value at all, while "" is a string with zero characters.
Click to reveal answer
What keyword in Swift represents the absence of a value?
✗ Incorrect
nil is the Swift keyword for no value.
How do you declare a variable that can hold a value or be nil?
✗ Incorrect
Adding ? after the type makes it optional and able to hold nil.
What happens if you try to use an optional variable without unwrapping it?
✗ Incorrect
Swift requires unwrapping optionals before use, else it gives a compile error.
Which of these safely unwraps an optional?
✗ Incorrect
if let unwraps the optional safely if it has a value.
What is the difference between
nil and an empty string ""?✗ Incorrect
nil means no value at all, while "" is an empty string.
Explain what
nil means in Swift and how you use it with optionals.Think about variables that might not have a value yet.
You got /3 concepts.
Describe how to safely use a variable that might be nil in Swift.
How do you check if a value exists before using it?
You got /3 concepts.