Clarification on assign, retain, copy, strong?

前端 未结 2 1923
心在旅途
心在旅途 2021-01-30 12:07

I\'m still new to Objective-C and having some difficulties trying to figure out the appropriate way to use assign, retain, copy, strong, etc. when setting a property.

F

2条回答
  •  梦毁少年i
    2021-01-30 12:36

    @property (nonatomic, copy) NSMutableArray *myArray
    @property (nonatomic, copy) NSString *myString
    @property (nonatomic, retain) UIColor *myColor
    @property (nonatomic) int myIn
    @property (nonatomic) BOOL myBOOL
    

    copy mutable objects, or objects with mutable subclasses such as NSString: this stops them being modified by another owner. although i don't think its recommended by apple to use mutable objects as properties

    other objects are generally retained, with an exception being delegates, which are assigned to prevent ownership loops

    primitives like int and BOOL are assigned, this is the default option for @property so doesnt need to be specified, although it doesnt hurt to add it in if you feel it helps the readability

提交回复
热议问题