Objective-C 101 (retain vs assign) NSString

前端 未结 8 1865
甜味超标
甜味超标 2020-11-29 17:19

A 101 question

Let\'s say i\'m making database of cars and each car object is defined as:

#import 

@interface Car:NSObject{
            


        
8条回答
  •  渐次进展
    2020-11-29 18:03

    After reading so many Articles, SO posts and made demo apps to check Variable property attributes, I decided to put all the attributes information together

    1. atomic //default
    2. nonatomic
    3. strong=retain //default
    4. weak= unsafe_unretained
    5. retain
    6. assign //default
    7. unsafe_unretained
    8. copy
    9. readonly
    10. readwrite //default

    so below is the detailed article link where you can find above mentioned all attributes, that will defiantly help you. Many thanks to all the people who give best answers here!!

    Variable property attributes or Modifiers in iOS

    1. retain = strong
      • it is retained, old value is released and it is assigned
      • retain specifies the new value should be sent -retain on assignment and the old value sent -release
      • retain is the same as strong.
      • apple says if you write retain it will auto converted/work like strong only.
      • methods like "alloc" include an implicit "retain"

    Example:

    @property (nonatomic, retain) NSString *name;
    
    @synthesize name;
    
    1. assign
      • assign is the default and simply performs a variable assignment
      • assign is a property attribute that tells the compiler how to synthesize the property's setter implementation
      • I would use assign for C primitive properties and weak for weak references to Objective-C objects.

    Example:

    @property (nonatomic, assign) NSString *address;
    
    @synthesize address;
    

提交回复
热议问题