The relationship of the instance variable (instance var) to the attribute (@property)
After Objective-c 2.0, declaring a @property name automatically produces an instance variable named _name, eliminating the hassle of repeating input for instance variables and properties. You can change the _name name by using @synthesize. @property and @synthesize do not have to appear in pairs.
@property Name: Instructs the compiler to automatically synthesize the setter and getter methods, the setter method name is SetName, and the Getter method name is name. @property The following keywords, such as ReadOnly, ReadWrite, retain, nonatomic, copy, and so on, to specify how the setter and Getter methods are produced. These property modifiers are broadly divided into four categories:
(1) variability (mutability)
-ReadOnly, Generate Getter method only, no setter method
-ReadWrite, is the default
(2) Memory management (management)
-Assign, is the default, applies to built-in types (int, bool, etc.) or proxy objects (delegate), there is no reference counting mechanism.
-Retain, applies only to objects, does not apply to built-in types (int, bool, etc.). When using the Setter method, add 1 to the object's reference count.
-Copy, when using the setter method, copies an object that creates a new object in memory instead of adding 1 to the reference count of the original object. Obviously, the reference count for the new object that was copied is 1.
(3) Concurrency (Concurrency)
-Nonatomic, Access attribute non-atomicity, general single-threaded declaration nonatomic, taking into account the speed problem. Do not use nonatomic for multi-threaded threads.
-Atomic, Access attribute atomicity, opposite to nonatomic.
(4) API controls (API control)
-Getter=newgettername, which specifies the new getter method name, typically rewrites the getter name of the BOOL instance variable. For example
[OBJC]View Plaincopy
- @property (getter=isfinished) BOOL finished;
-Setter=, specifying the new setter method name.
@synthesize name = Custom_name: Replace the instance variable _name name with Custom_name
@synthesize Name: Replace the instance variable _name
Note: @synthesize does not affect the name of the setter and getter methods produced by @property
When is the setter and getter method called?
For example, attribute declarations are as follows
[OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
- @interface Person:nsobject
- @property NSString *firstname; //Atomic, assign, ReadWrite (default)
- @end
There are 2 ways to do this:
(1) Show call (Send message)
[OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
- [Someperson FirstName]; //Call getter Method
- [Someperson Setfirstname: @ "Johnny"]; //Call setter method
(2) Implicit invocation (point syntax)
[OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
- NSString *firstname = Someperson. firstName; //Call getter Method
- Someperson. firstName = @ "Johnny"; //Call setter method
If you use instance variables directly in an instance method, the corresponding setter and getter methods are not called, for example
[OBJC]View Plaincopy < param name= "wmode" value= "Transparent" >
- -(void) SomeMethod
- {
- nsstring *mystring = _firstname; //Won ' t call getter method
- _FirstName = @ "A string"; //Won ' t call setter method
- }
Obviously using instance variables directly can be risky, such as memory leaks, circular references, and so on. The best way to access instance variables is through @property generated setter and getter methods.
The relationship of the instance variable (instance var) to the attribute (@property)