preface

The keyword of property property is often used in our daily development, so it is necessary for us to have a full understanding of it, so that we can know why it is used in our daily development.

Property keyword description

The property keyword falls into four categories:

  1. Atomicity:atomicandnonatomic.propertyThe default isatomicThat is, thread safety, but we generally usenonatomic. becauseatomicThread-safe system resource overhead is relatively high, affecting performance, and even if we need to use thread-safe, we can use other methods to achieve it. Atomic implementation is based ongetter,setterThe use of@synchronizedSynchronized lock key for code block lock and implementation.

Supplementary atomic is a spinlock, which means that if the previous thread has not finished executing (locked), the next thread will wait (will not go to sleep), and when the previous thread has finished executing, the next thread will execute immediately. It is different from a mutex, which goes to sleep while waiting and wakes up when the previous thread has finished executing.

  1. Reference counts are as follows: assign, retain, weak, strong, copy, and unsafe_unretained as used in iOS5. Assign: mainly modifies basic data types. It cannot modify object types, such as NSInterger and CGFloat. And unified memory management by the system stack. Retain: Decorates the object type, strongly references the object, and is an object reference count plus one, which can be used in MRC environments. Weak: Modifies the object type, weakly references the object, and does not increase the reference count of the object. If the object is destroyed, the pointer automatically points to nil, so you can prevent the problem of wild Pointers. Strong: modifies the object type and strongly references the object, increasing the reference count of the object. If it points to an empty object, it causes a wild pointer. Only available in ARC environments. Copy: When a new object is assigned with a reference count of 1, a copy of the incoming value is made, so the copy keyword is used. You assign an object to a property, which does not hold the object, but creates a new object and copies it to it. Objects that use the copy keyword must implement the NSCoding protocol. Unsafe_unretained: Similar to weak, a weak reference is declared, but if the reference count is 0, the variable is not automatically set to nil.

  2. Read/write: The default value is readWrite and readonly. When modifying a property, the property cannot be modified by outsiders.

  3. Method name: The name of the setter and getter method that sets the property. #### added to introduce weak keyword:

  • Usage scenario When some objects reference each other, avoid circular references caused by strong references. Objects cannot be released.
  • Realize the principle ofweakModification,runtimeWill maintain ahashTable (also known asweakTable) for storing all of the objectsweakA pointer,hashThe tablekeyIs the address of the object,valueforweakAn array of the addresses of Pointers whose value is the address of the object to which they point. (notestrongIs through theruntimeMaintain an automatic reference count table)weakSummary of the realization principle of
  1. At initialization,runtimeWill be calledobjc_initWeakFunction to initialize a new oneweakPointer to object address;
  2. When adding a reference,objc_initWeakThe function will callobjc_storeWeakThe function,objc_storeWeakIs used to update the pointer pointer and create the corresponding weak reference table (hash table).
  3. Release when calledclearDeallocatingFunction.clearDeallocatingThe function is first fetched based on the object addressweakArray of pointer addresses, and then iterate through that array and set the pointer that points to the empty object tonilAnd finally take the pointer fromweakDelete from table, and finally clean up the object’s record.

Copy and strong: The only thing we need to know about deep and shallow copies is that we can refer to them here. Specific examples are as follows:

@property (nonatomic, copy) NSMutableArray *mutArray;
NSMutableArray *mutArray1 = [NSMutableArray array];
self.mutArray = mutArray1;
Copy the code

Is equivalent to

@property (nonatomic, strong) NSMutableArray *mutArray;
NSMutableArray *mutArray1 = [NSMutableArray array];
self.mutArray = [mutArray1 copy];
Copy the code

After testing, we know that using a copy modified mutArray array, when its setter method is called, it creates a new object with a reference count of 1 and then frees the old object. A property modified by copy is assigned to an immutable array by copy. And the use of mutable array increment, delete, change, lookup function is found to find the relevant instance method and crash.

NSString use copy instead of retain.

@property (nonatomic, retain) NSString *string;
NSMutableString *string1 = [[NSMutableString alloc] initWithString:@"abc"];
self.string = string1;
[string1 appendString:@"123"];
NSLog(@"= = = = = = = = = = = = = = % @ = = = = = = = = =", self.string); The 2019-06-19 17:08:58. 114333 + 0800 ThinTableVIew1 [96955-2656816] = = = = = = = = = = = = = = abc123 = = = = = = = = =Copy the code

From the printed information you can see that when you change the value of string1, the value of self.string also changes. Let’s explore how this works by looking at the implementation of setter methods for string properties:

@property (nonatomic, retain) NSString *string;

- (void)setString:(NSString *)string {
    if(_string ! = string) { [_string release]; _string = [string retain]; //[string retain]; //_string = string; } } ================== @property (nonatomic, copy) NSString *string; - (void)setString:(NSString *)string {
    if (_string != string) {
        [_string release];
        _string = [string copy];
    }
}
Copy the code

We can know:

  • When usingretainmodifiedstringIs called_string = [string retain], which only adds a reference to string, and_stringPointers andstringIt points to the same piece of content. So changestringThe content will also change_stringThe content of the.
  • When usingcopymodifiedstringIs called when the object passed in is a mutable object[string copy]; Will create a new object and assign it to_string, so_stringandstringDon’t interfere with each other, and changestringThe content will not affect_string.