DEV Community

You know the difference about Weak and Unowned reference in iOS?

Recently, I was asked about this topic in a interview, and it's made me thinking about this subject, and realize that can pass unseen sometimes. Based on this, I decide write this article to help early iOS Developers, I going to unite two mine passions here, beyond iOS Development, that are Football and Pokémon, to exemplify.

Quick tip: Write subjects like that in post-it's, do smart notes always as a good way to fix the knowleadge and remember when we forget.

What is ARC?

Before talking about weak and unowned, it's important to understand ARC (Automatic Reference Counting). ARC is a system that Swift uses to automatically manage the memory of objects. It tracks how many times an object is referenced and deallocates it from memory when there are no more references to it.

Weak References

Imagine that Pikachu and Ash are friends on a Pokémon journey.

Weak Reference: Pikachu has a weak link with Ash. If Ash decides to leave, Pikachu doesn’t try to stop him or keep the space occupied by Ash.

class Pokemon {
    weak var friend: Pokemon?
}

var pikachu: Pokemon? = Pokemon()
var ash: Pokemon? = Pokemon()
pikachu?.friend = ash
ash?.friend = pikachu

pikachu = nil
// ash?.friend is now nil because pikachu was deallocated
Enter fullscreen mode Exit fullscreen mode

When to use weak?

Reference Cycles: We use weak when we want to avoid reference cycles. This happens when two objects reference each other, preventing ARC from deallocating either of them.

Avoiding Memory Leaks: Since weak doesn't keep the object in memory, it's ideal for situations where the referenced object can be deallocated without issues.

Unowned References

Now, imagine a soccer team with a coach.

Unowned Reference: The coach of the team has a link with a star player, believing that the player will always be there as long as the coach needs him. If the player gets transferred to another team, this could cause a problem.

class Player {
    var coach: Coach?
    init(coach: Coach) {
        self.coach = coach
    }
}

class Coach {
    unowned var starPlayer: Player
    init(starPlayer: Player) {
        self.starPlayer = starPlayer
    }
}

var messi: Player? = Player(coach: Coach(starPlayer: messi!))

messi = nil
// Accessing coach?.starPlayer will now cause an error because messi was deallocated
Enter fullscreen mode Exit fullscreen mode

When to use unowned?

Existence Guarantee: We use unowned when we're sure that the referenced object will be in memory as long as the reference exists.

Master-Detail Relationships: Ideal for situations where a main object (master) guarantees the existence of the referenced object (detail).

Summary

weak:
Doesn’t keep the object in memory.
Optional reference, can become nil.
Used when the referenced object’s lifecycle may be shorter.

unowned:
Doesn’t keep the object in memory.
Non-optional reference, never becomes nil.
Used when the referenced object’s lifecycle is guaranteed to be equal to or longer.

Conclusion

I hope this explanation helps clarify the difference between weak and unowned! Keep going strong in your learning journey, drink water, and always believe in yourself!

Top comments (0)