Objective-C 解析器与静态 getter 函数的问题
我创建了一个静态 getter 函数:
@implementation MyClass
static int aValue = 1;
+ (int) aValue { return aValue; }
// other stuff here
@end
现在我尝试以与另一个类不同的方式访问它:
@implementation AnotherClass
- (void) aMethod {
if (MyClass.aValue > 0) { NSLog(@"Foobar"); } // No parser error
if ((MyClass.aValue > 0)) { NSLog(@"Foobar"); } // My parser doesn't like that
if (([MyClass aValue] > 0)) { NSLog(@"Foobar"); } // This is ok again
if ((0|MyClass.aValue > 0)) { NSLog(@"Foobar"); } // Gives a warning, but works
}
// other stuff here
@end
如您所见,解析器似乎在嵌套布尔表达式中的静态方法方面存在问题,这是非常不幸的,如果你想使用 &&, ||及类似条款。
完整的 Xcode 错误消息是“在 '.' 之前预期有 ')'”令牌”。有人可以向我解释解析器的行为吗?我错过了一些重要的事情还是这是一个错误?
勒托比
I've created a static getter-function:
@implementation MyClass
static int aValue = 1;
+ (int) aValue { return aValue; }
// other stuff here
@end
and now I'm trying to access it in some different ways from another class:
@implementation AnotherClass
- (void) aMethod {
if (MyClass.aValue > 0) { NSLog(@"Foobar"); } // No parser error
if ((MyClass.aValue > 0)) { NSLog(@"Foobar"); } // My parser doesn't like that
if (([MyClass aValue] > 0)) { NSLog(@"Foobar"); } // This is ok again
if ((0|MyClass.aValue > 0)) { NSLog(@"Foobar"); } // Gives a warning, but works
}
// other stuff here
@end
As you can see the parser seems to have problems with static methods in nested boolean expressions, which is quite unfortunate, if you want to use &&, || and similar clauses.
The full Xcode error message is "Expected ')' before the '.' token". Can someone explain the parser behaviour to me? Am I missing something important or is this a bug?
Le Torbi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用@property关键字声明的属性仅在实例中有意义。
不存在阶级财产这样的东西。
您可以创建 getter 方法,但无法在类中使用点语法。
始终使用方法调用:
[ MyClass myValue ]
Properties, declared with the @property keyword, only have a meaning in instances.
There's no such thing as a class property.
You can create a getter method, but you won't be able to use the dotted syntax with a class.
Always use the method call:
[ MyClass myValue ]