0
0
Swiftprogramming~5 mins

Limitations and best practices in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a common limitation when using Swift's automatic reference counting (ARC)?
ARC can cause retain cycles if two objects hold strong references to each other, leading to memory leaks.
Click to reveal answer
beginner
Why should you avoid force unwrapping optionals in Swift?
Force unwrapping can cause runtime crashes if the optional is nil. It's safer to use optional binding or optional chaining.
Click to reveal answer
intermediate
What is a best practice when working with concurrency in Swift?
Use structured concurrency with async/await and avoid shared mutable state to prevent race conditions and improve code clarity.
Click to reveal answer
intermediate
What limitation does Swift have regarding dynamic dispatch for structs and enums?
Structs and enums use static dispatch by default, so you cannot override methods or use polymorphism like with classes.
Click to reveal answer
beginner
Why is it recommended to use value types (structs) over reference types (classes) when possible in Swift?
Value types provide safer code by avoiding shared mutable state and making data easier to reason about, improving performance and thread safety.
Click to reveal answer
What can cause a retain cycle in Swift's ARC system?
AUsing value types instead of reference types
BUsing optional binding
CTwo objects holding strong references to each other
DUsing async/await
Which is the safest way to access an optional value in Swift?
AForce unwrapping with !
BUsing a forced cast
CIgnoring the optional
DOptional binding with if let
What is a recommended practice when writing concurrent Swift code?
AUse structured concurrency and avoid shared mutable state
BAvoid async/await
CUse shared mutable state freely
DUse global variables for data sharing
Which Swift types use static dispatch by default?
AClasses
BStructs and enums
CProtocols
DClosures
Why prefer value types over reference types in Swift when possible?
AThey avoid shared mutable state and improve thread safety
BThey are slower but safer
CThey allow inheritance
DThey use dynamic dispatch
Explain the main limitation of Swift's ARC and how to avoid it.
Think about how two objects can keep each other alive unintentionally.
You got /4 concepts.
    Describe best practices for safely handling optionals and concurrency in Swift.
    Focus on safety and clarity in your code.
    You got /5 concepts.