Zproperty declaration attribute Declaration
Specifying the attribute (attributes) for instance variables allows the compiler to generate methods for accessing instance variables without leakage and thread security.
Property)
@property (copy, nonatomic) NSString *title;
What are the differences between assign, copy, and retain?
- Assign: simple value assignment without changing the index count (reference counting ).
- Copy: Create an object with an index count of 1 and release the old object.
- Retain: Release the old object, assign the value of the old object to the input object, and increase the index count of the input object to 1.
The actual retain syntax is:
- (void)setName:(NSString *)newName { if (name != newName) { [name release]; name = [newName retain]; // name’s retain count has been bumped up by 1 }}
The following is the most important thing:
? If you don't know how to use them, then->
- Use assign: for basic data types (nsinteger, cgfloat) and C data types (INT, float, double, Char, etc)
- Use copy: clone an object
- Use retain: for other nsobject and its subclass
Nonatomic Keyword:
Atomic is a thread protection technology used by objc. It basically prevents reading data by another thread when the write is not completed, resulting in data errors. This mechanism consumes system resources. Therefore, nonatomic is a good choice for small devices such as the iPhone, if multi-thread communication programming is not used.
@ Property is the corresponding compiler instruction.
Declare an attribute with the same name as a data member to save the declaration of a read/write function.
@ Interface Application
{
Unsigned int root_port;
Unsigned int notifier;
Uiwindow * window;
Mainview * mainview;
}
-(Void) applicationdidfinishlaunching :( ID) arg1;
-(Void) applicationwillsuspend;
-(Void) dealloc;
@ Property (retain) uiview * mainview; // @ synthesize mainview;
@ Property (retain) uiwindow * window; // @ synthesize window;
@ End
The syntax for declaring property is:
@ Property (parameter) type name;
The parameters here are mainly divided into three types:
Read/write attributes (readwrite/readonly)
Setter semantics (assign/retain/copy)
Atomicity atomicity (nonatomic)
Assign/retain/copy determines how to assign a new value to a data member.
The default value of Atomicity is atomic, And the READ function is atomic.
The frequently used parameter is copy/reain/assign. Select a setter to determine how the property is processed. In many objective-C objects, it is best to use retain. For some special objects (such as string), copy is used.
The assign keyword indicates that setter directly assigns a value, instead of copying or retaining it. This mechanism is very suitable for some basic types, such as nsinteger and cgfloat, or types you do not directly own, such as delegates.
The readonly keyword indicates that the setter will not be generated, so it cannot be used in combination with copy/retain/assign.
In implementation, you only need
@ Synthesize mainview;
@ Synthesize windows;
Instead of the tedious setter and getter methods, the compiler can automatically generate read/write functions.
Defines the property, the user, can point (.) to access the property, haha, a bit like C ++