使用getter =的点语法与方法语法 [英] dot syntax vs method syntax with getter=
问题描述
我不确定这个问题有多少用途,但对我来说似乎很有趣...
I'm not sure how much use this question is but it seems interesting to me...
我认为使用property/synthesize语句等效于创建getter/setter.因此
I thought that using property/synthesize statements was equivalent to me creating the getter/setter. Therefore
// .h
@property (nonatomic) BOOL on;
// .m
@synthesize on = _on;
// In my mind synthesizes the following methods
// - (BOOL)on;
// - (void)setOn:(BOOL)on;
但是,如果我将声明更改为以下内容:
However if I change the declarations to the following:
v
@property (nonatomic, getter=isOn) BOOL on;
@synthesize on = _on;
// In my mind synthesizes the following
// - (BOOL)isOn;
// - (void)setOn:(BOOL)on;
然后鉴于以上所述,我覆盖了吸气剂,所以我知道它何时被调用:
Then given the above I override the getter so I know when it is called:
- (BOOL)isOn;
{
NSLog(@"I was called");
return _on;
}
现在在实例(myClass
)上调用以下内容会导致:
Now calling the following on an instance (myClass
) results in:
NSLog(@"%d", [myClass isOn]);
//=> 2012-02-09 22:18:04.818 Untitled[1569:707] I was called
//=> 2012-02-09 22:18:04.820 Untitled[1569:707] 1
NSLog(@"%d", myClass.isOn);
//=> 2012-02-09 22:18:24.859 Untitled[1599:707] I was called
//=> 2012-02-09 22:18:24.861 Untitled[1599:707] 1
NSLog(@"%d", myClass.on); // This is the one I didn't expect to work
//=> 2012-02-09 22:18:55.568 Untitled[1629:707] I was called
//=> 2012-02-09 22:18:55.570 Untitled[1629:707] 1
我一直认为,如果我在这种意义上使用属性,那么使用带有点语法的getter/setter格式是完全有效的
I had always assumed that if I was using a property in this sense it was perfectly valid to use the getter/setter with dot syntax in the form
myClass.isOn;
myClass.on = on;
从另一个问题建议使用点语法时,应使用如下属性名称:
From another question it was suggested that when using dot syntax I should use the property name like this:
myClass.on // Correct
myClass.isOn // Incorrect
尽管这可行,但逻辑上似乎有点不合逻辑,因为我知道没有底层方法- (BOOL)on
,而是将其映射到- (BOOL)isOn
Although this works it seem slightly less logical because I know there is no underlying method - (BOOL)on
it is instead mapped to - (BOOL)isOn
我的问题是(使用后面的示例)
My questions are (using the latter example)
- 这是错误还是应该
myClass.on
真正更改为调用- (BOOL)isOn
- 从严格意义上讲,我正在访问状态而不是调用行为,所以我当前使用的点语法正确吗? (例如
myClass.isOn
)
- Is this a bug or should
myClass.on
really be silently changed to call- (BOOL)isOn
- Semantically speaking I am accessing state not invoking behaviour so is my current use of dot syntax correct? (e.g.
myClass.isOn
)
尽管没有人明确地说过,我认为使用.isOn
是不好的形式,因为无论在幕后调用相同方法的事实如何,语义上isOn
都在问一个问题,这是更多的行为,而不是比国家.
Although no one has explicitly said it I have reasoned that using .isOn
is bad form because regardless of the fact that under the hood the same method is called, semantically isOn
is asking a question, which is more behaviour rather than state.
但是,我仍然不清楚魔术"接线的位置,从而将对myClass.on
的调用转换为[myClass isOn]
However I am still unclear on where the "magic" wiring goes on that turns calls to myClass.on
into [myClass isOn]
After looking around the docs some more I found this section on Declared Properties. Using the following code I can inspect a class' properties:
id MyClass = objc_getClass("MyClass");
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(MyClass, &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
NSLog(@"Name: %s, attributes: %s\n", property_getName(property), property_getAttributes(property));
}
//=> 2012-02-10 07:10:28.333 Untitled[934:707] Name: on, attributes: Tc,GisOn,V_on
因此,我们具有以下属性:
So we have the following attributes:
- name = on
- type = char(Tc)
- getter = isOn(GisOn)
- 变量= _on(V_on)
所有这些信息都可以在运行时使用时,问题就出在运行时还是在编译时进行了查找,就像一些答案所建议的那样?
With all of this information available at runtime it kind of leaves the question is this lookup done at runtime or compile time like some answers suggest?
推荐答案
但是,我仍然不清楚魔术"接线的位置,从而将对myClass.on的调用转换为[myClass isOn]
However I am still unclear on where the "magic" wiring goes on that turns calls to myClass.on into [myClass isOn]
在获取上下文中编译obj.name时,逻辑肯定如下:
The logic surely goes as follows, when compiling an obj.name in a getting context:
if(there is an accessible @property for name in scope)
{
if(there is a custom getter specified)
compile "[obj customGetter]"
else
compile "[obj name]"
}
else if (there is an an accessible instance method name in scope)
compile "[obj name]"
else
{
compile "[obj name]"
warn obj may not respond to name
}
语言/执行环境可以通过其他方式处理自定义getter名称,但是鉴于Obj-C将声明放在标头(公共)中,以上是关于自定义getter逻辑在何处执行的很好的猜测-编译呼叫站点时.
There are other ways a language/execution environment can handle custom getter names, but given that Obj-C puts the declaration in the header (which is public) the above is a good guess as to where the custom getter logic is performed - when compiling the call site.
这篇关于使用getter =的点语法与方法语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!