Objective-C类别:是否可以为不在我的类别中的方法添加属性? [英] Objective-C categories: Can I add a property for a method not in my category?
问题描述
我想使用类别使原始类上的方法也可以用作属性.
I want to use a category to make a method on the original class available as a property as well.
A级:
@interface ClassA
- (NSString*)foo;
@end
A类类别
@interface ClassA (Properties)
- (void)someCategoryMethod;
@property (nonatomic, readonly) NSString *foo;
@end
现在,当我这样做时,它似乎可以工作(也许不起作用,它没有抱怨但我看到了陌生感),但是它给了我警告,因为我没有在我的类别实现中综合属性.我怎么告诉编译器实际上一切正常,因为原始类为我合成了该属性?
Now when I do this, it seems to work ( Maybe it doesn't work, it doesn't complain but I am seeing strangeness), but it gives me warnings because I am not synthesizing the property in my category implementation. How do I tell the compiler everything is actually just fine since the original class synthesizes the property for me?
推荐答案
以下是您得到的警告:
warning: property ‘foo’ requires method '-foo' to be defined - use @synthesize, @dynamic or provide a method implementation
要取消显示此警告,请在您的实现中添加它:
To suppress this warning, have this in your implementation:
@dynamic foo;
这篇关于Objective-C类别:是否可以为不在我的类别中的方法添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!