Using property in obj-C (use of properties in obj-C)
Basic Format:
1 @ InterfaceYourmotherobject: yourmothersmotherobject2 {3...//Declare member variables4 }5@ Property(Attribute Value) member variable type member variable name6 @ End
Attribute Value introduction (too lazy to write, copy ):
Readonly
This tag indicates that the attribute is read-only, and the default tag is read/write. If you specify read-only, you only need one reader in @ implementation. Or if you use the @ synthesize keyword, the reader method is parsed. And if you try to assign values to attributes using the vertex operator, you will get a compilation error.
Readwrite
This tag indicates that the attribute is read/write, which is also the default attribute. Both the configurator and reader must be implemented in @ implementation. If the @ synthesize keyword is used, the reader and the setter are parsed.
Assign
This flag indicates that the setter directly assigns a value, which is also the default value. Applications that use garbage collectionProgramIf you want an attribute to use assign and the class complies with the nscopying protocol, you need to specify this tag rather than simply using the default value. Otherwise, you will get a compilation warning. This again shows to the compiler that you really need to assign values, even if it is copyable.
Retain
The specified retain will wake up the retain message of the input value when the value is assigned. This attribute can only be used for the objective-C object type, but not for the core foundation object. The reason is obvious: retain will increase the reference count of the object, and neither the basic data type nor the core foundation object has reference count. In fact, if you use the retain attribute for the basic data type or core Foundation object, a compilation error is returned.
Copy
It indicates that a copy of the input value is used when the value is assigned. The copy operation is executed by the copy method. This attribute is only valid for object types that implement the nscopying protocol. For more information, see "copy.
Nonatomic
It indicates that accessors are not atomic operations, but by default, accessors are atomic operations. That is to say, in a multi-threaded environment, the parser provides a secure access to the attributes. The returned values obtained from the reader or the values set by the setter can be completed at one time, even other threads are accessing it. If you do not specify nonatomic, the resolved accessors retain and automatically release the returned values in the memory management environment. If nonatomic is specified, the accessors simply return this value. In a non-multithreading environment, atomic operations are not required to improve efficiency.
Example:
@ Interface Yourmotherobject {unsigned Int Yourmother; unsigned Int Yoursister; uiwindow * Window; mainview * Mainview ;} -( Void ) Applicationdidfinishlaunching :( ID ) Arg1; -( Void ) Applicationwillsuspend; -( Void ) Dealloc; @ property (retain) uiview * Mainview;// @ Synthesize mainview; @ Property (retain) uiwindow * window; // @ Synthesize window; /* Can't be compiled @ property (retain) unsigned int yourmother; */ /* OK */ @ Property (nonatomic, Readwrite ) Unsigned int yoursister; @ End
In implementation, you only need
@ Synthesize mainview;
@ Synthesize windows;
@ Synthesize yoursister;
Instead of the tedious setter and getter methods, the compiler can automatically generate read/write functions and use dot notation (.) to access attributes.
For example, self. Yoursister = 3 will actually call [self setyoursister: 3]. The setyoursister method is to notify the compiler of automatically generated @ synthesize yoursister statement. The yoursister method (same as the variable name, that is, Getter) is also generated. You can also use the following method to generate the corresponding method, and use the @ dynamic method name.
@ Property (nonatomic, readwrite) unsignedIntYoursister; @ dynamic yoursister;-(UnsignedInt) Yoursister {//Any code...}
Note that in the implementation of the class, directly using the member variable name (yoursister above) will directly assign values to the member variable, while using self. member variable name or object. the member variable name calls the property generation method. Note that in obj-C, the default access permission for member variables is protected. You can also use @ public, @ private, and @ protected to specify the display.
Additional content:
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.