Accessors
存取方法
All instance variables are private in Objective-C by default, so you should use accessors to get and set values in most cases. There are two syntaxes. This is the traditional 1.x syntax:
OC 中全部的实例变量默认是私有的。所以多数情况下你应该使用訪问器来获得和设置实例变量的值。
訪问器有两种语法。以下说的是传统的
1.x 版本号: [photo setCaption:@"Day at the Beach"];
output = [photo caption];
The code on the second line is not reading the instance variable directly. It's actually calling a method named caption. In most cases, you don't add the "get" prefix to getters in Objective-C.
上面的第二行代码不是直接读取实例变量值,实际上是调用了名叫caption的方法。多数情况下,你不要在OC的取值方法中加入"get"前缀。Whenever you see code inside square brackets, you are sending a message to an object or a class.
每当你看到中括弧中得代码时,你正在给一个类或实例对象发送消息。
Dot Syntax
点语法
The dot syntax for getters and setters is new in Objective-C 2.0, which is part of Mac OS X 10.5:
存取方法的点语法是在 OC 2.0 版中作为Mac OS X 10.5的一部分新增加的。
photo.caption = @"Day at the Beach";
output = photo.caption;
You can use either style, but choose only one for each project. The dot syntax should only be used setters and getters, not for general purpose methods.
你能够使用以上两种方式,但一个项目仅仅能选用一种方式。点语法不适用于普通用意的方法。仅仅能用作设值和取值方法。也就是存取方法。
原文: part 2