[Arrangement] Setter: assign, copy, retain, weak, and strong of Property in Object-C, differences and connections between object-csetter
During iOS programming, we often see some modifiers before some attributes, such as copy and retain.
These keywords are the setter of Property in Object-C language.
Mac Official Website:
The Objective-C Programming Language-Declared Properties-Setter Semantics
The explanation in is:
Setter SemanticsThese attributes specify the semantics of a set accessor. They are mutually exclusive.
-
strong
-
Specifies that there is a strong (owning) relationship to the destination object.
-
weak
-
Specifies that there is a weak (non-owning) relationship to the destination object.
If the destination object is deallocated, the property value is automatically set to nil
.
(Weak properties are not supported on OS X v10.6 and iOS 4; use assign
instead.)
-
copy
-
Specifies that a copy of the object should be used for assignment.
The previous value is sent a release
message.
The copy is made by invoking the copy
method. This attribute is valid only for object types, which must implement the NSCopying
protocol.
-
assign
-
Specifies that the setter uses simple assignment. This attribute is the default.
You use this attribute for scalar types such as NSInteger
and CGRect
.
-
retain
-
Specifies that retain
should be invoked on the object upon assignment.
The previous value is sent a release
message.
In OS X v10.6 and later, you can use the __attribute__
keyword to specify that a Core Foundation property should be treated like an Objective-C object for memory management:
@property(retain) __attribute__((NSObject)) CFDictionaryRef myDictionary;
Even after reading the explanation, it is difficult to understand the specific meaning.
Later I read a lot of people's summary, and I went through some practical programming, especially:
[Resolved] the iOS program has a warning: ARC Semantic Issue, Assigning retained object to unsafe property; object will be released after assignment, and an error occurred while running the program: Thread 1: EXC_BAD_ACCESS (code = 1, address = 0xe0000010)
It is understandable.
As follows:
To understand the meanings of the setter, you must first understand the ARC in Object-C.
With regard to ARC, a few simple words are: the compiler will automatically help you implement the reference technology, so you don't have to bother writing retain, release, and other words to worry about the reference technology.
For more details about ARC, refer:
[Summary] Automatic Reference Counting (ARC, Automatic Reference Counting) for iOS)
Strong and weak
With ARC, you can use weak or strong to describe whether the attribute is weak or strongly referenced;
Assign, retain, and copy
Before ARC is available, assign, retain, and copy are used to modify attributes.
Assign is mainly used for numeric variables, that is, scalar, which can be directly assigned with a value, without involving changes in reference count (scalar value, and no reference technology is available for management );
Copy: Copies a new object. The reference count is reset to 1 to release the old object;
Retain: Adds 1 to the reference count of the original object and does not release the old object;