How do weak and strong references look like in objective-c?

后端 未结 2 829
孤独总比滥情好
孤独总比滥情好 2020-12-28 18:54

Wikipedia states \"In computer programming, a weak reference is a reference that does not protect the referenced object from collection by a garbage collector\". How do thos

2条回答
  •  失恋的感觉
    2020-12-28 19:39

    A weak reference is a reference that isn't strong enough to force an object to remain in memory while a strong reference forces an object to remain in memory.

    If you have created weak reference to any variable, you may get nil for that.

    UITableViewDelegate, UIScrollViewDelegate, etc are examples of weak references.

    Example of Strong reference :

    MyClass *obj1 = [[Myclass alloc] init];
    
    Myclass *obj2 = obj1;
    

    Here obj2 has strong reference to obj1 mean if you remove obj2 from memory then obj1 also get removed.

提交回复
热议问题