0
0
iOS Swiftmobile~5 mins

Optionals and unwrapping in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an Optional in Swift?
An Optional is a type that can hold either a value or no value (nil). It is used to handle the absence of a value safely.
Click to reveal answer
beginner
How do you declare an Optional String variable in Swift?
You add a question mark after the type, like this: var name: String? This means name can hold a String or nil.
Click to reveal answer
beginner
What does 'unwrapping' an Optional mean?
Unwrapping means accessing the actual value inside an Optional safely, checking if it is not nil before using it.
Click to reveal answer
intermediate
Explain 'optional binding' with an example.
Optional binding uses <code>if let</code> or <code>guard let</code> to check if an Optional has a value and then use it safely. Example: <code>if let safeName = name { print(safeName) }</code>
Click to reveal answer
intermediate
What happens if you force unwrap a nil Optional?
Force unwrapping a nil Optional causes a runtime crash. Use ! only when you are sure the Optional has a value.
Click to reveal answer
How do you declare an Optional integer in Swift?
Avar number: Int!
Bvar number: OptionalInt
Cvar number: Int?
Dvar number: Int
Which keyword is used for safe unwrapping of an Optional?
Aunwrap let
Bunwrap
Cforce
Dif let
What does the exclamation mark (!) do when used with an Optional?
AForce unwraps an Optional
BSafely unwraps an Optional
CDeclares an Optional
DMakes a variable constant
What will happen if you force unwrap a nil Optional?
AApp crashes
BReturns default value
CCompiler error
DReturns nil
Which of these is NOT a way to unwrap an Optional?
AOptional binding with if let
BDeclaring with var
CUsing guard let
DForce unwrapping with !
Explain what an Optional is in Swift and why it is useful.
Think about how you handle missing or unknown values in real life.
You got /4 concepts.
    Describe two ways to unwrap an Optional safely in Swift.
    These ways help you avoid crashes by checking if the value exists first.
    You got /4 concepts.