dot语法如何在Objective-C中没有显式的@property? [英] How does dot syntax work without explicit @property in Objective-C?
问题描述
我写了一个setter和getter方法遵循苹果的惯例,并注意到,尽管没有变量,我仍然可以访问setter和getter使用点语法。这是正常的行为吗?
//头定义。请记住,对于height,没有类变量或@property。
- (void)setHeight:(float)height;
- (float)height;
// else使用点语法。
object.height = 10.0f;
属性访问表达式等同于消息表达式:
[object setTexture:tex];
属性声明等效于一个属性声明( readonly
)或两个( readwrite
/ default)实例方法声明。关键字如 retain
告诉编译器如果你告诉编译器这样做( @synthesize
)如何实现该方法。 / p>
但是,您可以跳过属性声明并直接声明方法,如您的问题所示。你不能综合它们的实现,因为你需要一个属性声明(否则,它不知道使用什么内存管理策略: assign
, retain
或 copy
),但您始终可以自行实施这些方法。
然后,即使您自己声明并实现了这些方法,由于属性访问语法和消息语法彼此相同,您可以使用任何您想要的方法:使用消息表达式或属性访问表达式。
有些人会认为它的形式不好,除了正式的 @property
(例如, myString.length
或 myArray.count
或 myView.frame
)。使用属性访问表达式发送不访问任何类型的属性的消息绝对是糟糕的形式; foo.retain.autorelease
,例如,是错误的:它试图假装你编程一些其他语言比Objective-C。
顺便说一下,一个属性和一个变量是不相关的。 A @property
通常由实例变量支持,但这不是必需的:您可以将属性的值存储在另一个对象中,或将其转换为其他格式, 或两者。同样,访问一个属性(它是一个访问器消息)和访问一个实例变量(只是访问一个变量,没有更多)是非常不同的。
I wrote a setter and getter method following Apple's conventions and noticed that despite having no variable I can still access the setter and getter using the dot syntax. Is this normal behavior? What enables this feature?
Example:
// Header definition. Keep in mind there is no class variable or @property for height.
- (void)setHeight:(float)height;
- (float)height;
// else using the dot syntax.
object.height = 10.0f;
A property-access expression is equivalent to a message expression:
[object setTexture:tex];
A property declaration is equivalent to one (readonly
) or two (readwrite
/default) instance-method declarations. Keywords like retain
tell the compiler how to implement the method if you tell it to do so (@synthesize
).
However, you can skip the property declaration and declare the methods directly, as shown in your question. You can't synthesize their implementations, since you need a property declaration for that (otherwise, it wouldn't know what memory-management policy to use: assign
, retain
, or copy
), but you can always implement the methods yourself.
Then, even though you declared and implemented the methods yourself, since property-access syntax and message syntax are equivalent to each other, you can use the methods whichever way you want: With a message expression, or with a property-access expression.
Some would consider it bad form, though, to use property access expressions on anything but a formal @property
(e.g., myString.length
or myArray.count
or myView.frame
). It definitely is bad form to use a property-access expression to send a message that doesn't access any kind of property; foo.retain.autorelease
, for example, is bad and wrong: It reeks of trying to pretend you're programming some other language than Objective-C.
Incidentally, a property and a variable are unrelated. A @property
will ordinarily be backed by an instance variable, but this is not required: You could store the property's value inside another object, or convert it to and from some other format, or both. Likewise, accessing a property (which is an accessor message) and accessing an instance variable (which is just accessing a variable, nothing more) are very different.
这篇关于dot语法如何在Objective-C中没有显式的@property?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!