Objective-C ARC: strong vs retain and weak vs assign

后端 未结 8 2428
眼角桃花
眼角桃花 2020-11-22 15:59

There are two new memory management attributes for properties introduced by ARC, strong and weak.

Apart from copy, which is ob

8条回答
  •  日久生厌
    2020-11-22 16:06

    Strong:

    • Property will not Destroy but Only once you set the property to nil will the object get destroyed
    • By default all instance variables and local variables are strong pointers.
    • You use strong only if you need to retain the object.
    • We generally use strong for UIViewControllers (UI item's parents)
    • IOS 4 (non-ARC) We Can Use Retain KeyWord
    • IOS 5(ARC) We Can Use Strong Keyword

    Example: @property (strong, nonatomic) ViewController *viewController;

    @synthesize viewController;

    Weak

    By Default automatically get and set to nil

    • We generally use weak for IBOutlets (UIViewController's Childs) and delegate
    • the same thing as assign, no retain or release

    Example : @property (weak, nonatomic) IBOutlet UIButton *myButton;

    @synthesize myButton;

提交回复
热议问题