11.1 Using attribute values
@propertyfloat rainHandling;//表明此类具有float类型的属性,其名称为rainHandling
Note: The name of the property does not have to be the same as the instance variable name.
@synthesize rainHandling;//表示创建了该属性的访问代码
Note : After you use a property, you can declare the instance variable without declaring it.
There are two places where you can add an instance variable declaration:
- Header file: Let subclasses access variables directly through properties
- Implementation file: The variable belongs to the current class only
Point expression:
- The point expression appears to the left of (=): Setter method
- The point expression appears to the right of (=): Getter method
Note : If the object accessed by the Access property prompt is not a struct type, check that the current class already contains the required header file
11.2 Property Extensions
Properties also apply to int,char,bool,struct types, or you can define a property of a Nsrect object
Preserve the Dead loop (retain cycle): The car object owns the engine object, but the engine object cannot have the car object in turn.
Copy feature
- (void)setName: (NSString *)newName { [name release]; // setName- (NSString *)name { return// name
Equivalent to:
//name属性是copy特性@propertyNSString *name;@synthesize name;
Retain features :
- How to access the engine:
- (void) setEngine: (Engine *) newEngine{ [newEngine retain]; [engine release]; engine = newEngine;} // setEngine- (Engine *) engine{ return (engine);} // engine
Equivalent to:
//engine属性使用的是保留和释放特性@property (retain) Engine *engine;@synthesize engine;
noatomic features : iOS programs often use the technology to get better performance on devices with limited resources. If you define an access method, you must use the Nonatomic attribute.
default attribute : If no attributes are specified for the property, they use the nonatomic and assign attributes by default.
11.21 use of the name
the name of the property is typically the same as the instance variable name that supports the property.
Sometimes the programmer wants the instance variable to be a name, while the exposed property is another name.
@property(copy)NSString *name;@synthesize name = appellation;// 编译器将创建-setName:和-name方法,但在实现代码中用的是appellation实例变量。
Note :
Direct access to instance variables: self.name
To assign a value using an access method:[self setName:@"Car"];
11.22 Read-only properties
For values that can only be read, cannot be changed:
@property(readonly)float shoeSize;
Note : Only one getter method is generated and no setter method is generated
11.23 Hands on your own
@dynamic
Tell the compiler not to generate any code or create the appropriate instance variable.
Change method Name:
You can customize the name of the method you want by using the getter= and setter= attributes
@propertyBOOL hidden;//告诉编译器生成名为isHidden的getter方法,并生成名为默认setHidden:的setter方法
Note : Attributes only support alternatives to the-setblah and-blah methods, but do not support methods that require additional parameters to be received
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Study note-objective-c] "objective-c-Basic Tutorial 2nd Edition" 11th Chapter properties