Several iOS attribute keywords
1.nonatomic,atomiac:
Nonatomic: nonatomic, atomiac atomic. The default attribute is atomiac, that is, atomic. Nonatomic has high execution efficiency.
atomiac: reading and writing is safe, but inefficient, not absolutely safe.
2.readwrite,readonly:
Readwrite read-write, readonly read-only. The default property is readwrite, which supports reading and writing.
readwirte: property has both set and get methods.
readonly: the property has only get methods.
3.assign:
You can modify both basic data types and object types; setter method is implemented by direct assignment, which is generally used for basic data types; Modify basic data types, such as NSInteger, BOOL, int, float, etc; When decorating an object type, its reference count is not increased; A dangling pointer will be generated (dangling pointer: after the object decorated with assign is released, the pointer still points to the original object address, and the pointer becomes a dangling pointer. At this time, if you continue to access the original object through this pointer, the program may crash).
strong
Strongis the default attribute keyword of an object. This attribute indicates that the attribute defines a "holding relationship". When setting a new value for this attribute, the setting method will first retain the new value, release the old value, and then set the new value. At this time, it is the same address as the original object. When the original object is a variable object, assign the original object to the immutable object modified by strong modify the original object, and our immutable object will change accordingly
copy
The ownership relationship expressed by this attribute is similar to strong. Then, the setting method does not retain the new value, but copies it. copy modifies immutable objects. Strong modifies variable objects.
- Copy modifies an immutable object. When the original object is immutable, assigning the original object to the attribute will copy the original object. At this time, it is a shallow copy, and the two pointers point to the same address.
- Copy modifies an immutable object. When the original object is a variable object, assigning the original object to an attribute will copy the original object. At this time, it is a deep copy. The two objects point to different addresses. The attribute refers to a copy of the variable object. If the original object is modified, it will not affect the value of the attribute.
- Copy modifies a variable object. If the original object is an immutable object and assigned to an attribute, it is a shallow copy, pointing to the same address.
- Copy modifies a variable object. If the original object is a variable object and assigned to an attribute, a deep copy is performed. The attribute points to an immutable copy of the original object, that is, the attribute is an immutable object. When adding, deleting or modifying an attribute, an error will be reported because a method cannot be found.
Example:
#import <Foundation/Foundation.h> @interface test : NSObject @property (nonatomic, copy) NSString *stringCopy; @property (nonatomic, strong) NSString *stringStrong; @property (nonatomic, copy) NSMutableString *mutableStringCopy; @end
// main.m int main(int argc, const char * argv[]) { @autoreleasepool { test * test1 = [[test alloc] init]; NSString *string = @"222"; test1.stringCopy = string; test1.stringStrong = string; NSLog(@"%p %p %p", string, test1.stringCopy, test1.stringStrong); NSMutableString *string2 = [[NSMutableString alloc] initWithString:@"111"]; test1.stringCopy = string2; test1.stringStrong = string2; NSLog(@"%p %p %p", string2, test1.stringCopy, test1.stringStrong); [string2 appendString:string]; NSLog(@"%@ %@ %@", string2, test1.stringCopy, test1.stringStrong); } return 0; }
Through the program to actually see what the difference is.
Output results:
0x100004008 0x100004008 0x100004008 0x1005229a0 0x84e16ab6357c8f7c 0x1005229a0 111222 111 111222
When the assignment type is NSMutableString, because the copy method performed on NSMutableString is a deep copy, which opens up new memory, the memory address of the attribute modified by copy has changed, while the strong is only the reference count + 1, and the memory address is still the source string. Therefore, the change will not affect the attribute modified by copy, but will change the attribute modified by strong. When the source string is an immutable type, copy and strongchange the source string with the same effect; When the source string is of variable type, copy modifies deep copy. Changing the source string does not affect the attribute content of copy; StrongThe modified attribute changes as the source string changes.
Use copy for variables of NSMutableString type, such as:
@property (nonatomic, copy) NSMutableString *mutableStringCopy;
Do the following:
test1.mutableStringCopy = string2; [test1.mutableStringCopy appendString:@"222"];
The program will crash with an error:
'-[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0xb04010f44c5ef9bd'
This is because the object obtained during assignment is an immutable object, and there must be no method of variable type for the immutable object. You can read about it in another blog: OC Foundation framework object replication.