I have encountered this problem over and over either in interviews or during my development. When would you want to use weak or unowned in swift.  To dig deeper into this issue lets first understand what ARC stands for and what its purposes are. If you are a savy apple developer and have started working with ARC you know the painful days of calling release and managing our own memory. Well ARC, Automatic Reference Counting does exactly what it stands for. It counts each reference as your objects are created and decrements the count as they are destroyed and no longer in use. Drumroll.. here is where Strong and weak come into account. As you create an object you increase the reference count by 1 and therefore are creating a strong reference. In Objective-C you create strong references overtime you instantiate a new object, calling self in a closure, or when you copy or add an object to an array. 

The following example all illustrates the way to create strong references in objc and swift
        var strongObject:AnyObject? = nil  
        var closure:((Void)->Void)? = nil

When the closure is called self is called inside the closure a strong reference is created to the closure

        closure{ self.begin()} 
Same applies in objective c when using blocks and calling self within the block.  
  strongObject* strObj =  [strongObject alloc]init] 
             strongObject* strObj =  [strongObject copy]
             strongObject* strObj =  [strongObject new]
             NSMutableArray* arr = [NSMutableArray alloc]initWithObject:strObj]

The responsibility of ARC is to watch when the reference count goes to 0 and at that time deallocate your object.  In Objective-C
      we have weak , retain, copy.  The real purpose of using weak is as follows: In cases of calling self in a closure the closure holds a strong reference to self and self hold a strong reference to he closure. ARC doesn’t know if you done using the object because there is now a retain cycle happening where one object points to the other object and the other object points to the first object. To break this cycle you add create the following:


 __weak self weakSelf = self; 



      In swift you do the following:


 closure{[weak self] in } 
   There is also another case where a retain cycle is created: When you create a class and have a child class that also has a strong 
   reference the child has a strong reference to the parent and vice versa. To break the cycle you do the following:


    @interface ParentClass
    __weak ChildClass* child; 


    class ParentClass{
    weak var childclass:ChildClass?
      There is one more case where strong references can cause a havoc. Whenever you create a delegate you want to make your 
      delegates weak:


   @protocol someProtocol
   @required -(void)successData;
   @interface ParentClass



    protocol someProtocol:class{
       func successData()
    class ParentClass{
       weak var delegate:someProtocol?
 Now you ask when do I use Unowned in swift?  remember that swift has optional values which are values that can be nil. If you  use [weak self]. The self inside the block is an optional , if you were to use [unowned self] then the self is a non optional. For other uses  just replace weak with unowned where every weak is used.In summary you learned about ARC and when to use weak and unowned. Please let me know if you have any suggestions or comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>