I. Introduction to Attributes
// property is OC2.0 and then a new syntax that allows us to quickly generate Setter as well Getter method that greatly simplifies the code
second, how to Define a property
//@property Property keyword to define the property
//nsstring * Types of properties
//name The name of the property.
//@property just the generatedSetteras wellGetterthe declaration of the method.
@property NSString *name;
third, the use of property considerations
//if the. Mwe did it ourselves in the file.Setteras wellGettermethod, the compiler will no longer help us buildSetterand theGettermethod, and the underlined instance variable is not automatically generated. //@synthesize when specifying an instance variable for a property name, go to find out if the instance variable is defined for the class, if it is defined, it is used directly, and if there is no definition, a specified instance variable is automatically generated .
// remember that auto-generated instance variables are private and subclasses are inaccessible.
// If the instance variable wants the subclass to be accessed, it must be . h defined in the file.
//@synthesize used toSetter as well Getterimplementation of the method//name Property name = instance variable name
// tells the compiler to automatically generate Setter as well Getter method is implemented internally, which is the instance variable that is manipulated by the
//when@synthesizeThe auto-generated instance variable and property name are the same when the property name does not specify the instance variable name to manipulate//if even@synthesizeis omitted, the compiler automatically generates an instance variable name that is Underline + The property name.
//@synthesize name = _name, sex = _sex, age = _age, weight = _weight;
Four, attributes of a property :
1. Read and write features:
getter method, do not generate setter method.
(both readable and writable) generates getter method, also generate setter readwrite
// (3) getter = method name, telling the compiler to generate getter method, the method name is the specified method name, if not specified, the default
// (4) setter = method name, telling the compiler to generate setter method, the method name is the specified method name, if not specified, the default set +
2. Atomic characteristics:
//(1) Atomic atomicity, which guarantees thread safety, means that only one thread can be accessed at the same time, there is a lock process, after which the thread accesses the end, there is a process of unlocking, the default atomic attribute is atomic.
//(2) nonatomic non-atomic, not guaranteed thread-safe, but more efficient thanAtomica lot higher and generally use no problem because we're in the use ofSetterand theGettermethods can sometimes be particularly frequent, using theAtomiccan seriously affect the efficiency of the operation, so Apple's official recommendation for atomic properties is recommendednonatomic.
3. Semantic features
//(1) Assign (default) simple assignment, mainly for the basic data type, sometimes can also be targeted to the object type;
1 for object type (only for objects)
//(3) Copy This semantic attribute copies the object and then holds ownership of the new object, only for the object
Five, different semantic characteristics of setter and getter method of the system internal implementation (we can only understand)
if the semantic attribute of the attribute is declared as Assign , the internally generated Setter The method is:
-(void) SetName: (NSString *) name {
_name = name;
//}
//getter Method
-(NSString *) name {
return _name;
//}
// if the semantic attribute of the attribute is declared as retain , the internally generated Setter The method is:
-(void) SetName: (NSString *) name {
if (_name! = name) {
[_name release];
_name = [name retain];
// }
//}
Getter
-(NSString *) name {
return [[_name retain] autorelease];
//}
// if the semantic attribute of the attribute is declared as Copy , the internally generated Setter The method is:
-(void) SetName: (NSString *) name {
if (_name! = name) {
[_name release];
_name = [name copy];
// }
//}
//getter Method
-(NSString *) name {
return [[_name retain] autorelease];
//}
This article is from the "A Mao" blog, please be sure to keep this source http://winann.blog.51cto.com/4424329/1436058