Bird
Raised Fist0
Swiftprogramming~5 mins

Weak references to break cycles in Swift - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a strong reference cycle in Swift?
A strong reference cycle happens when two or more objects hold strong references to each other, preventing them from being deallocated and causing a memory leak.
Click to reveal answer
beginner
How does a weak reference help break a strong reference cycle?
A weak reference does not increase the reference count of an object. This allows one object to refer to another without preventing it from being deallocated, thus breaking the cycle.
Click to reveal answer
beginner
In Swift, how do you declare a weak reference?
You declare a weak reference by using the keyword weak before the property declaration, like weak var delegate: SomeDelegate?.
Click to reveal answer
intermediate
Why must weak references be optional in Swift?
Because the referenced object can be deallocated at any time, weak references must be optional to allow them to become nil automatically when the object is gone.
Click to reveal answer
intermediate
What happens if you use a strong reference instead of a weak reference in a delegate pattern?
Using a strong reference in a delegate pattern can create a strong reference cycle between the delegate and the delegator, causing both to stay in memory and leading to a memory leak.
Click to reveal answer
What keyword do you use in Swift to declare a weak reference?
Aweak
Bstrong
Cunowned
Dvar
Why must weak references be optional in Swift?
ABecause they are constants
BBecause they always hold a value
CBecause they increase the reference count
DBecause they can become nil when the referenced object is deallocated
What problem does a strong reference cycle cause?
AMemory leak by preventing deallocation
BFaster program execution
CAutomatic deallocation
DSyntax errors
Which of these is NOT a way to break a strong reference cycle?
AUsing weak references
BUsing strong references everywhere
CUsing unowned references
DSetting references to nil
In a delegate pattern, what kind of reference should the delegate property usually have?
AUnowned reference
BStrong reference
CWeak reference
DConstant reference
Explain what a strong reference cycle is and how weak references help to solve it in Swift.
Think about how objects keep each other alive and how weak references avoid that.
You got /5 concepts.
    Describe how to declare a weak reference in Swift and why it must be optional.
    Focus on the syntax and behavior of weak references.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main purpose of using weak references in Swift?
      easy
      A. To increase the performance of the program by caching values
      B. To prevent strong reference cycles and allow objects to be deallocated
      C. To make variables constant and unchangeable
      D. To create multiple copies of an object automatically

      Solution

      1. Step 1: Understand what strong reference cycles are

        Strong reference cycles happen when two objects hold strong references to each other, preventing deallocation.
      2. Step 2: Role of weak references

        Weak references do not increase the reference count, so they help break these cycles and allow memory to be freed.
      3. Final Answer:

        To prevent strong reference cycles and allow objects to be deallocated -> Option B
      4. Quick Check:

        Weak references prevent cycles = C [OK]
      Hint: Weak references avoid memory cycles by not owning objects [OK]
      Common Mistakes:
      • Thinking weak makes variables constant
      • Confusing weak with performance optimization
      • Assuming weak creates copies
      2. Which of the following is the correct syntax to declare a weak reference to an instance of class Person in Swift?
      easy
      A. weak var friend: Person?
      B. weak let friend: Person?
      C. weak var friend: Person
      D. var weak friend: Person?

      Solution

      1. Step 1: Understand weak reference syntax

        Weak references must be declared with weak var and must be optional because the referenced object can be nil.
      2. Step 2: Check each option

        weak var friend: Person? uses weak var friend: Person?, which is correct syntax.
      3. Final Answer:

        weak var friend: Person? -> Option A
      4. Quick Check:

        Weak references use 'weak var' and optional type = A [OK]
      Hint: Weak references are always optional vars [OK]
      Common Mistakes:
      • Using 'let' with weak
      • Not making the type optional
      • Placing 'weak' after 'var'
      3. Consider the following Swift code snippet:
      class Owner {
          var pet: Pet?
      }
      
      class Pet {
          weak var owner: Owner?
      }
      
      let owner = Owner()
      let pet = Pet()
      owner.pet = pet
      pet.owner = owner
      
      print(owner.pet!.owner! === owner)

      What is the output of this code?
      medium
      A. nil
      B. false
      C. true
      D. Compilation error

      Solution

      1. Step 1: Understand the references

        The Owner has a strong reference to Pet, and Pet has a weak reference back to Owner.
      2. Step 2: Evaluate the print statement

        The expression owner.pet!.owner! === owner checks if the pet's owner is the same instance as owner. Since pet.owner weakly references owner, this is true.
      3. Final Answer:

        true -> Option C
      4. Quick Check:

        Weak reference points back correctly = true [OK]
      Hint: Weak references still point to original object if alive [OK]
      Common Mistakes:
      • Thinking weak reference becomes nil immediately
      • Confusing identity check with equality
      • Expecting compilation error due to weak
      4. Given the following code, why does it cause a strong reference cycle?
      class A {
          var b: B?
      }
      
      class B {
          var a: A?
      }
      
      let a = A()
      let b = B()
      a.b = b
      b.a = a

      How can you fix this cycle?
      medium
      A. Change a in class B to a weak reference
      B. Change b in class A to a weak reference
      C. Make both references strong
      D. Use unowned for both references

      Solution

      1. Step 1: Identify the cycle

        Class A holds a strong reference to B, and B holds a strong reference back to A, causing a cycle.
      2. Step 2: Break the cycle with weak reference

        Changing the reference from B to A to weak var a: A? breaks the cycle because B no longer strongly owns A.
      3. Final Answer:

        Change a in class B to a weak reference -> Option A
      4. Quick Check:

        Break cycle by making one side weak = D [OK]
      Hint: Make one side weak to break cycles [OK]
      Common Mistakes:
      • Making both references weak (loses ownership)
      • Making the wrong reference weak
      • Using unowned incorrectly causing crashes
      5. You have two classes, Teacher and Student, where each Teacher has many Students and each Student has one Teacher. How should you declare the references to avoid strong reference cycles?
      hard
      A. Teacher holds weak references to Students; Student holds a strong reference to Teacher
      B. Both hold weak references
      C. Both hold strong references
      D. Teacher holds strong references to Students; Student holds a weak reference to Teacher

      Solution

      1. Step 1: Analyze ownership

        A Teacher owns many Students, so it should hold strong references to them.
      2. Step 2: Avoid cycle by weak reference

        Each Student should hold a weak reference back to its Teacher to avoid a strong cycle.
      3. Final Answer:

        Teacher holds strong references to Students; Student holds a weak reference to Teacher -> Option D
      4. Quick Check:

        Owner strong, back weak = B [OK]
      Hint: Owner holds strong, back reference is weak [OK]
      Common Mistakes:
      • Making owner reference weak
      • Making both references strong causing cycles
      • Making both references weak losing ownership
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard