The iOS development detail properties are set to readwrite readonly retain copy assign nonatomic

  • 2020-05-12 06:15:36
  • OfStack

Please refer to the following text for detailed introduction. This article is very detailed.

1. Readability: readonly, readwrite

@property(readwrite,....) valueType value;

This property is the default property of the variable, which means that if you (readwrite and readonly is not used, then your variable is readwrite), your variable will have get and set methods by adding the readwrite property.

property(readonly,...) valueType value;

This property variable indicates that the variable has only readable methods, that is, you can only use its get methods.

2. assign, setter methods are directly assigned without any retain operations, in order to solve the original type and circular reference problems

3. The retain, setter method performs the old release value and the new retain value on the parameters, in this order for all implementations

4. copy, setter method carries out Copy operation, which is similar to retain process 1. First, the old value release, and then copy generates a new object, retainCount being 1. This is a mechanism introduced to reduce reliance on context.

5.nonatomic, non-atomic access, asynchronous, multi-threaded concurrent access to improve performance.

Note that without this property, the default is that both access methods are atomic transaction accesses. The lock is added to the level of the owned object instance. So no nonatomic is safe for multithreading.

6 . retain vs. Copy

copy: creates an object with an index count of 1, then releases the old object
retain: releases the old object, assigns the value of the old object to the input object, and increases the index count of the input object to 1

What the hell does that mean?

copy is actually creating the same object, whereas retain is not:

For example, define the following properties:


@property (copy, nonatomic) NSString *testStr;

Usage:


 NSMutableString *str3 =[[NSMutableString alloc ]initWithString:@"Mutable String"
self.testStr = str3;
NSLog(@"%d", [self.testStr retainCount]);
NSLog(@"%d", [str3 retainCount]);

You can see that testStr and str3 are different addresses, and retainCount is both 1

If you change copy to retain, they point to the same address; retainCount is 2.
You see, retain is the pointer copy to the same address, counting plus 1, while copy copies the content.

Objective-C properties (assign, retain, copy, readonly, readwrite, atomic, nonatomic)

assign: specifies a simple assignment for the setter method, which is the default. You can use this property for scalar types such as int. You can imagine an float, it's not an object, so it can't retain, copy.

retain: specifies that retain should be called on a subsequent object, with the first value sending an release message. You can imagine an instance of NSString, which is an object, and you might want to retain it.

copy: specifies that a copy of the object (a deep copy) should be used, with the first value sending an release message. Basically like retain, but without increasing the reference count, is allocating 1 block of new memory to place it.

readonly: only the getter method will be generated and not the setter method (the getter method does not have the get prefix).

readwrite: the default property that generates the getter and setter methods with no additional parameters (the setter method has only one parameter).

atomic: the default property for an object, setter/getter, is an atomic operation. If there are multiple threads calling setter at the same time, there will not be a situation in which one thread executes setter before the other thread executes the entire setter statement, related to the method with a lock 1 added at the beginning and end.

nonatomic: the atomicity of setter/getter is not guaranteed. Data may be problematic in multi-threaded situations.

The above content is the iOS development details property Settings readwrite, readonly, retain, copy, assign, nonatomic.


Related articles: