0
0
Swiftprogramming~5 mins

Optional declaration with ? suffix in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the ? suffix mean when declaring a variable in Swift?
It means the variable is an optional. It can hold a value or be nil (no value).
Click to reveal answer
beginner
How do you declare an optional integer variable named age in Swift?
You write: var age: Int? This means age can hold an integer or be nil.
Click to reveal answer
intermediate
What happens if you try to use an optional variable without unwrapping it?
Swift will not let you use it directly because it might be <code>nil</code>. You must unwrap it safely first.
Click to reveal answer
intermediate
How can you safely check if an optional has a value before using it?
You can use if let or guard let to unwrap the optional safely.
Click to reveal answer
beginner
What is the difference between Int and Int? in Swift?
Int must always have a value. Int? can have a value or be nil.
Click to reveal answer
What does var name: String? mean in Swift?
Aname is an integer
Bname must always be a String
Cname is a constant String
Dname can be a String or nil
How do you declare an optional Double variable called price?
Avar price: Double?
Bvar price: Double
Clet price: Double?
Dprice: Double
What must you do before using an optional variable's value?
AAssign it a new value
BUnwrap it safely
CConvert it to a string
DNothing special
Which keyword helps safely unwrap an optional in Swift?
Afor
Bswitch
Cif let
Dwhile
What value can an optional variable hold that a non-optional cannot?
Anil
B0
Cempty string
Dfalse
Explain what an optional variable is in Swift and why we use the ? suffix.
Think about variables that might not have a value yet.
You got /4 concepts.
    Describe how to safely use the value inside an optional variable.
    How do you check if the optional has a value before using it?
    You got /4 concepts.