How to Use Object-c @ property
Property is a code generation mechanism that can generate different types of getter/setter functions, especially if you want to use vertex (.) to access a variable, you must use the property.
How to use it?
Usage example: @ property (attribute1, attribute2) float value;
These attributes include:
Readonly-Read-Only: Read-only, but not set value (the setXXXX function cannot be used ).
Readwrite-Readable and writable (default ).
Assign-Replace the old and new variables (default) when setting values ).
Retain-When the value is set, the new variable is retain, and the old variable is release.
Copy-Copy a new variable when setting the value, and release the old variable.
Nonatomic-The default value is atomic.
Strong-In the reference counting environment, the default value is strong, which works the same as retain;
Weak was introduced from System 5.0, which is similar to assign. However, when the referenced object is 0, it is automatically set to nil.
The first two are just simple settings of variable readability.
Assign is a simple replacement variable, which is usually used in scalar types, such as NSInterger and CGRect,
Or (in the reference counting environment) for objects you do not own, such as delegates.
In the garbage collection environment, retain and assign are actually the same.
The content of the setter code generated by it is similar:
-(Void) setValue :( float) newValue {
Value = newValue;
}
Another important thing to note is that when using property, you must add self (such as self. xxx) to the front. If you do not do this, it is easy to cause memory leakage.