In the previous note is to learn how to manually write getter and setter method, relatively complex, today learned the @property keyword can automatically generate these two methods, very convenient,
In addition, in memory management, you can also use this keyword, eliminating the need to rewrite the Dealloc function of the trouble, just need to write in the key words can.
Let the system automatically merge setter and getter for as long as two steps:
(1). Use the @property directive to define properties in the Class interface section.
(2). Declare the attribute in the part of the class implementation using the synthesize directive.
All, the access method of the engine defined in the example above can be modified to the following code:
The original setter method and getter method can be combined to change to:
1/**/2 @interface Car:nsobject3 {engine;< Span style= "color: #008080;" >5 Tire *tires[4]; // Four wheels, defines an array of four numbers. 6 }7 @property (nonatomic) Engine* Engine;8 @end // Car
The implementation method is as follows:
@implementation Car2 @synthesize engine; @end;
In the use of @property instructions, the back will take some parameters, the specific role of the general introduction. If you want to know more, you can Baidu after yourself.
Atomic (nonatomic): Specifies whether the stored method of the composition is an atomic operation. That is, the main point is linear security. Aomic can guarantee object data integrity, but thread security is reduced. Nonatomic can improve the access performance of storage methods.
Copy: When the setter method is called to assign a value to a member variable, the assigned object is copied to a copy, and the copy is assigned to the member variable. The copy instruction avoids the object being cleared when the value of the counter is 0.
readonly: Indicates that the system only synthesizes getter methods and no longer synthesizes setter methods. That is: The defined method is read-only and cannot be assigned.
ReadWrite: is the default value indicating that the system needs to synthesize the Setter,getter method.
retain: When an object is assigned to the property, the reference number of the object referenced by the property is reduced by 1, and the number of references to the assigned object is added to the value 1. (used in ARC memory mechanism)
Retaincount: Gets the number of references to the referenced object.
Weak: indicator Specifies that the property holds a weak reference to the assigned object. That is, even if the weak reference points to an assigned object, the object may be recycled .
Strong: indicator Specifies that the property holds a strong reference to the object being assigned. That is, the object is not automatically reclaimed as long as it refers to the assigned object.
Wei iOS Basic Learning Note ten OC language Basics -05 @property keywords