0
0
Swiftprogramming~5 mins

Nil represents absence of value in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Avoid
Bnull
Cnone
Dnil
How do you declare a variable that can hold a value or be nil?
Avar name: String
Bvar name: Optional
Cvar name: String?
Dvar name: nil
What happens if you try to use an optional variable without unwrapping it?
ACompile-time error
BSwift crashes at runtime
CIt works fine
DIt prints nil
Which of these safely unwraps an optional?
Aprint(optionalValue)
Bif let value = optionalValue { ... }
Cvar value = optionalValue
DoptionalValue = nil
What is the difference between nil and an empty string ""?
A<code>nil</code> means no value, <code>""</code> is a string with no characters
BThey are the same
C<code>nil</code> is a string, <code>""</code> is nil
D<code>nil</code> means 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.