Skip to content

Commit 779e38e

Browse files
committed
Unowned
Add Unowned on Memory Management
1 parent 7247473 commit 779e38e

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Use the Table of Contents to practice and test your knowledge. It doesn't show t
3434
- [Memory Management](https://github.com/onthecodepath/iOS-Interview-Questions#memory-management)
3535
- Why do you generally create a weak reference when using self in a block?
3636
- What is memory management handled on iOS?
37-
- What is the difference between *weak* and *strong*?
37+
- What is the difference between *weak*, *strong* and *unowned*?
3838
- What is a memory leak?
3939
- What is a retain cycle?
4040
- What is the difference between copy and retain?
@@ -225,12 +225,13 @@ SomeBlock* __weak weakSelf = self;
225225

226226
iOS uses something called ARC which stands for Automatic Reference Counting. When an object is said to have a strong reference to it, ARC increase its retain count by 1. When the retain count of an object reaches 0, the object will typically be deallocated if there are no more strong references to it. Unlike garbage collection, ARC does not handle reference cycles automatically.
227227

228-
#### What is the difference between *weak* and *strong*?
228+
#### What is the difference between *weak*, *strong* and *unowned*?
229229

230230
First, objects are *strong* by default.
231231

232232
- *Strong* means that the reference count will be increased and the reference to it will be maintained through the life of the object.
233-
- *Weak*, means that we are pointing to an object but not increasing its reference count. It’s often used when creating a parent child relationship. The parent has a strong reference to the child but the child only has a weak reference to the parent. ([source](https://medium.com/ios-os-x-development/ios-interview-questions-13840247a57a))
233+
- *Weak*, means that we are pointing to an object but not increasing its reference count. It's always optional. It’s often used when creating a parent child relationship. The parent has a strong reference to the child but the child only has a weak reference to the parent. ([source](https://medium.com/ios-os-x-development/ios-interview-questions-13840247a57a))
234+
- *Unowned* is the same as *Weak* in terms of reference counting but *Unowned* can be non-optional. It's useful when you are sure the object will not be accessed when it's nil (in this case it would cause an Error) because *Unowned* references don't require nil checking.
234235

235236
Common instances of *weak* references are delegate properties and subview/controls of a view controller's main view since those views are already strongly held by the main view. ([source](http://stackoverflow.com/questions/11013587/differences-between-strong-and-weak-in-objective-c))
236237

0 commit comments

Comments
 (0)