# Weak vs Strong IBOutlets

**URL:** <https://forums.kodeco.com/t/weak-vs-strong-iboutlets/114950>\
**Category:** Auto Layout by Tutorials\
**Created:** [July 15, 2020, 7:37am UTC](https://forums.kodeco.com/t/weak-vs-strong-iboutlets/114950 "2020-07-15T07:37:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pulkitap](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pulkitap/32/81494_2.png) [@pulkitap](https://forums.kodeco.com/u/pulkitap)\
**Post date:** [July 15, 2020, 7:37am UTC](https://forums.kodeco.com/t/weak-vs-strong-iboutlets/114950/1 "2020-07-15T07:37:40Z")

</div>

Hi,

Generally when we create IBOutlets, they are weak, but in book(Chapter 2), we are using strong.  
Is it intentional or some printing error? And if it is intentional, then what is the difference btw weak and strong IBOulets?

Thanks

---

<div class="post-metadata">

**Author:** ![jayvenn](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayvenn/32/100175_2.png) [@jayvenn](https://forums.kodeco.com/u/jayvenn)\
**Post date:** [July 15, 2020, 6:19pm UTC](https://forums.kodeco.com/t/weak-vs-strong-iboutlets/114950/2 "2020-07-15T18:19:08Z")

</div>

Hi @pulkitap!

Although IBOutlets are often found as weak, Apple recommends a while back (2015) that references should be strong by default. Of course, this is unless the reference needs to be weak. However, it seems like this recommendation hasn’t had a significant impact enough caught on. I’ve attached a WWDC15 video and Apple’s documentation regarding IBOutlet storage type below for reference.

WWDC 2015 (Video 407, Timestamp 32:31):

> **[Implementing UI Designs in Interface Builder - WWDC15 - Videos - Apple Developer](https://developer.apple.com/videos/play/wwdc2015/407/)**
>
> Apps that stand out must be attractive, intuitive, and easy to use. Interface Builder experts will give you the skills to turn your idea...

Apple’s Documentation:

> **[Nib Files](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html)**
>
> Explains how to work with nib and bundle resources in apps.

Hope I’ve answered your question!

---

<div class="post-metadata">

**Author:** ![pulkitap](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pulkitap/32/81494_2.png) [@pulkitap](https://forums.kodeco.com/u/pulkitap)\
**Post date:** [July 16, 2020, 4:46am UTC](https://forums.kodeco.com/t/weak-vs-strong-iboutlets/114950/3 "2020-07-16T04:46:57Z")

</div>

Thanks @jayvenn for ur quick reply.

As u mentioned that “references should be strong by default. Of course, this is unless the reference needs to be weak.”, can we follow the reverse approach of this, i.e take IBOutlets as weak reference, unless we do not need it as strong?

---

<div class="post-metadata">

**Author:** ![jayvenn](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayvenn/32/100175_2.png) [@jayvenn](https://forums.kodeco.com/u/jayvenn)\
**Post date:** [July 17, 2020, 3:11am UTC](https://forums.kodeco.com/t/weak-vs-strong-iboutlets/114950/4 "2020-07-17T03:11:43Z")

</div>

Hi @pulkitap !  
Defaulting IBOutlets to strong and only changing IBOutlets to weak as needed is recommended over the other way around. Imagine the way you’d initialize view properties programmatically. You’d also start with strong references and only change properties to weak when needed.

---

<div class="post-metadata">

**Author:** ![pulkitap](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pulkitap/32/81494_2.png) [@pulkitap](https://forums.kodeco.com/u/pulkitap)\
**Post date:** [July 17, 2020, 5:07am UTC](https://forums.kodeco.com/t/weak-vs-strong-iboutlets/114950/5 "2020-07-17T05:07:24Z")

</div>

Hi @jayvenn

Your logic sounds good to me, but I am having one more query, i.e. If we follow the strong IBOutlets approach, then do not we need to always check for memory that whether our views are in any kind of retain cycle or not? But if we follow the weak IBOutlet approach, then we do not specifically check for retain cycle.

Thanks

---

<div class="post-metadata">

**Author:** ![jayvenn](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayvenn/32/100175_2.png) [@jayvenn](https://forums.kodeco.com/u/jayvenn)\
**Post date:** [July 20, 2020, 4:25am UTC](https://forums.kodeco.com/t/weak-vs-strong-iboutlets/114950/6 "2020-07-20T04:25:47Z")

</div>

Hi @pulkitap!

The best practice is to start with a strong reference and move to weak as needed. When determining if a property should be strong or weak, look into Apple’s Automatic Reference Counting (ARC) first introduced in iOS 5 and 2011. You can learn more about this topic with the following article:

[https://www.raywenderlich.com/966538-arc-and-memory-management-in-swift](https://www.raywenderlich.com/966538-arc-and-memory-management-in-swift)

And/or with the following video course:

[https://www.raywenderlich.com/1940727-advanced-swift-memory-management](https://www.raywenderlich.com/1940727-advanced-swift-memory-management)

Understanding Swift memory management and reference counting will help you decide whether an IBOutlet should be weak or strong. You can use the initializer and de-initializer to verify whether class instances have the correct allocation/deallocation behavior with the reference count expectations. Beyond that, running Allocations and Leaks instruments against your application can help you find less obvious memory leaks.

Xcode understands that properties are referenced from the Interface Builder with the IBOutlet property declaration. Beyond that, the fundamentals of Automatic Reference Counting (ARC) and memory management in Swift still applies.

Every view controller has a strong reference to a view it manages. If you open `UIViewController`, you can find that `view` is declared as strong. You want a strong reference to mitigate invalid memory deallocation. When a view is alive, you should ask if its subviews should also be alive? In most cases, this would be a yes. Hence, the view keeps a strong reference to its subviews. All in all, the compiler needs to know when memory deallocation of a class instance is safe to mitigate memory overflow.

As a bonus resource, if you’d like to watch Chris Lattner introduce Automatic Reference Counting from about nine years ago, you can watch his introduction of the memory management system here from 2011:

> **[Design and Development Videos - Apple Developer](https://developer.apple.com/videos/)**
>
> Learn about developing for Apple platforms with video presentations by Apple experts.

---

<div class="post-metadata">

**Author:** ![kgboreham](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kgboreham/32/94984_2.png) [@kgboreham](https://forums.kodeco.com/u/kgboreham)\
**Post date:** [July 22, 2020, 2:31pm UTC](https://forums.kodeco.com/t/weak-vs-strong-iboutlets/114950/7 "2020-07-22T14:31:41Z")

</div>

Recommendations do change over time and a lot has changed in the last five years. This is what the link above says today -

> You typically need strong references to top-level objects to ensure that they are not deallocated; you don’t need strong references to objects lower down in the graph because they’re owned by their parents, and you should minimize the risk of creating strong reference cycles.  
> Outlets that you create should therefore typically be `weak`

---

<div class="post-metadata">

**Author:** ![jayvenn](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayvenn/32/100175_2.png) [@jayvenn](https://forums.kodeco.com/u/jayvenn)\
**Post date:** [August 20, 2020, 8:26am UTC](https://forums.kodeco.com/t/weak-vs-strong-iboutlets/114950/8 "2020-08-20T08:26:17Z")

</div>

Perhaps this situation can also be expressed this way:

> Top-level objects typically need to be strong to avoid deallocation. Objects down the graph don’t typically need to be strong to mitigate the chances of creating strong reference cycles.

I’d say this is still a rule of thumb. Understanding Swift memory management and reference counting should help you definitively decide whether an IBOutlet should be weak or strong.
