0
0
Swiftprogramming~5 mins

Unowned references for guaranteed lifetime in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an unowned reference in Swift?
An unowned reference is a non-owning pointer to an instance that is expected to always have a valid value during its lifetime. It does not increase the reference count and assumes the referenced object will not be deallocated while the unowned reference exists.
Click to reveal answer
intermediate
How does an unowned reference differ from a weak reference?
An unowned reference assumes the referenced object will never be nil during its lifetime, so it is non-optional and does not become nil automatically. A weak reference is optional and automatically becomes nil when the referenced object is deallocated.
Click to reveal answer
beginner
When should you use unowned references?
Use unowned references when you know the referenced object will always outlive the reference, such as in parent-child relationships where the child references the parent but the parent owns the child.
Click to reveal answer
intermediate
What happens if you access an unowned reference after the referenced object is deallocated?
Accessing an unowned reference after the referenced object is deallocated causes a runtime crash because the reference points to invalid memory.
Click to reveal answer
beginner
Example: How do you declare an unowned reference in Swift?
You declare an unowned reference using the keyword <code>unowned</code> before the variable, for example: <br><pre>class Child {
  unowned let parent: Parent
  init(parent: Parent) {
    self.parent = parent
  }
}</pre>
Click to reveal answer
What keyword declares an unowned reference in Swift?
Aunowned
Bweak
Cstrong
Doptional
Which statement about unowned references is true?
AThey do not increase the reference count and are non-optional.
BThey are always optional types.
CThey become nil automatically when the referenced object is deallocated.
DThey increase the reference count to keep the object alive.
When is it safe to use an unowned reference?
AWhen the referenced object might be deallocated anytime.
BWhen the referenced object is guaranteed to outlive the reference.
CWhen you want the reference to become nil automatically.
DWhen you want to create a strong ownership cycle.
What happens if you access an unowned reference after its object is deallocated?
ANothing happens; it continues normally.
BThe reference becomes nil safely.
CSwift automatically recreates the object.
DThe program crashes at runtime.
Which of these is a typical use case for unowned references?
AA parent object referencing its child.
BTwo unrelated objects referencing each other strongly.
CA child object referencing its parent.
DA global variable referencing a local variable.
Explain what an unowned reference is and when you should use it in Swift.
Think about parent-child relationships and memory safety.
You got /5 concepts.
    Describe the difference between unowned and weak references and why that difference matters.
    Consider what happens if the referenced object is deallocated.
    You got /5 concepts.