Objective-C Properties java
Apple introduced properties, a combination of new compiler directives
and a new attribute accessor syntax. spring
Apple 引入了屬性這一律念。新的編譯器命令和新的屬性存取句法。 多線程
1.1 Shrinking the Interface 減小interface代碼 app
@property float rainHandling; says that objects of the class AllWeatherRadial have an attribute, of type float, called rainHandling. It also says that you can set the property by calling –setRainHanding: and that you can access the attribute by calling -rainHandling. ide
@property float rainHandling;這個說明這個對象有一個類屬性,是float 類型,名字爲rainHandling 。一樣,你可以經過調用setRainHanding 設置屬性值。 this
1.2 Shrinking the Implementation atom
@synthesize rainHandling; spa
@synthesize snowHandling; 線程
@synthesize is a compiler feature that says "create the accessors for this attribute." code
@ synthesize是編譯器特點,說建立這個屬性的獲取。
@synthesize is not code generation. You won't ever see the code that implements –setRainHandling: and –rainHandling, but these methods will exist and will be callable.
@synthesize 不是代碼生成器。可是這項方法存在,且能被調用。
If you provide the methods in your class for the property, the compiler doesn't create them. The compiler only creates methods that are missing.
若是你提供了這個屬性的一個方法,那麼compiler將不建立這些方法。compiler
只建立缺失的方法。
Most properties are backed by a variable, so when you synthesize the getter and setter, the compiler automatically creates an instance variable with the same name as the property.
大部分properties 返回一個variable ,因此當你同步getter 和setter方法的時候,compiler 自動建立了與這個屬性名字相同的實例變量。
Notice that the header file has two variables called rainHandling and snowHandling. The setter and getter will use these variables. If you don't declare those variables, the compiler creates them.
注意頭文件有兩個變量,若是沒寫的話,compiler將自動添加這兩個方法。
1.3 Dot notation
The dot notation looks a lot like structure access in C and object access in Java—on purpose.
點運算符想存儲在C和對象存取在java中同樣
1.4 Objecting to Properties 對象 對於屬性
Objects bring some added complications.
對象讓事情麻煩了。
Recall that we retain and release objects as they flow through our accessors.
當他們要通過accessor時,咱們retain 和release 對性。
For some object values, particularly string values, you want to always -copy them. Yet for other object values, like delegates (which we'll talk about in the next chapter), you don't want to retain them at all.
像string你想copy 它,而對於delegate ,你卻不想retain 他們。
@property (copy) NSString *name;
@property (retain) Engine *engine;
You can use some other decorations, like nonatomic, which makes accessors a bit faster if they won't be used in a multithreaded environment.
若是 他們不用在多線程環境,nonatomic 使得accessor更快,
s. You can also use assign if you don't want the attribute object to be retained, to help avoid retain cycles.
若是你不想屬性對象被retained ,那麼咱們將盡可能避免retian 循環。
You can only specify retain and copy attributes for retainable pointers (i.e. Objective-C objects). All other types, such as C and nonretainable pointers, must use assign and manage memory manually.
If you provide either or both the setter and getter yourself, you cannot use atomic attribute; you must use nonatomic.
若是你本身寫了setter 或getter方法中任意一個,那麼你不能用atomic 屬性,必須使用nontomic 屬性。
1.5 Appellation spring 別名
the name of the property has been the same as the name of an instance variable that backs that property.
通常狀況下,property的名字應該和返回的property的實例變量名字同樣。
Sometimes, though, you may want one name for the instance variable and another for the public attribute name.
可是有些時候你想讓實例變量一個名字,而公共屬性名字爲另外一個。
@interface Car : NSObject
{
NSString *appellation;
NSMutableArray *tires;
Engine *engine;
}
@property (copy) NSString *name;
@property (retain) Engine *engine;
and then change the synthesize directive: @synthesize name = appellation;
In init, change
name = @"Car";
to
self.name = @"Car";
What's that self-dot-name business? It's a bit of disambiguation to let the compiler know that we want to vector through the accessors. If we just use a naked name, the compiler assumes that we're directly modifying an instance variable. To go through the accessors, we can write [self setName:@"Car"]. Remember that the dot is just shorthand for making this exact same call, so self.name = @"Car" is just another way of saying the same thing.
只是爲了去除歧義。
1.6 Read-Only About It只讀
You might have an object with an attribute that is read-only.
有一些屬性你容許它只能讀。
You can code for these situations with more attributes on @property.
你能夠在@property中加attribute
By default, properties are mutable: you can read and write them. Properties have a readwrite attribute you can use for specifying this.
默認狀況下,properties 是可變的。你能讀和寫他們。
@property (readonly) float shoeSize;
@property (readonly) NSString *licenseNumber;
When the compiler sees that @property is readonly, it generates a getter but not a setter for that attribute.
當compiler 看到@property 只讀時,應該只產生getter方法,而不產生setter 方法。
1.7
What if you'd rather not have a variable, getter, and setter for your property?
若是你getter 和setter方法都不想要的話怎麼辦?
You can use the keyword @dynamic to tell the compiler not to generate any code or create a variable for the property.
你能夠用關鍵則@dynamic 來告訴編譯器不產生任何代碼。
@property (readonly) float bodyMassIndex;
@dynamic bodyMassIndex;
- (float)bodyMassIndex
{
///compute and return bodyMassIndex
}
1.8 chang the method name
you might not like the names of the methods that are generated by default. They're in the form of blah and setBlah:.
有時候你想用本身的方法名字。
To overcome this, you can specify the names of the getter and setter that the compiler generates. Use the attributes getter= and setter= to define the preferred names of your methods.
爲此,你應該在@property設置getter= and setter attribute .
@property (getter=isHidden) BOOL hidden;
1.9 properties 並非無所不能
- (void) setTire: (Tire *) tire atIndex: (int) index;
- (Tire *) tireAtIndex: (int) index;
these methods don't fit into the fairly narrow range of methods that properties cover. Properties will let you replace only –setBlah and –blah methods, but not methods that take extra arguments, like the tire's position on the car.
上面的方法並不適合properties .由於屬性僅僅容許你替換setter和getter 方法不帶參數的部分。