公开只读但具有私有设置程序的Objective-C属性 [英] Objective-C property that is readonly publicly, but has a private setter
问题描述
我想使用@property
语法声明一个综合属性,该属性是公开只读的,但具有可以在该类内部私下调用的setter.
I'd like to use the @property
syntax to declare a synthesized property that is publicly readonly but has a setter that can be called privately from within the class.
由于它是Objective-C,因此从本质上讲这意味着setFoo:
方法将被综合,但是在类本身之外调用它会导致警告(无法识别的选择器).要触发警告,我必须声明属性readonly
;有什么方法可以强制仅在课程中可用的合成二传手?
Since it's Objective-C, this basically means that the setFoo:
method would be synthesized, but calling it outside of the class itself would result in a warning (unrecognized selector). To trigger the warning I have to declare the property readonly
; is there any way to force a synthesized setter that is only available within the class?
推荐答案
我认为您正在寻找的被称为类扩展.您将在标头中将属性声明为只读:
I think what you're looking for are called class extensions. You would declare the property read-only in the header:
@interface MyClass : NSObject {
}
@property (readonly, assign) NSInteger myInteger;
@end
然后在实现文件中的类扩展中重新声明:
Then redeclare in your class extension in the implementation file:
@interface MyClass ()
@property (readwrite, assign) NSInteger myInteger;
@end
@implementation MyClass
@end
For more check out Apple's documentation
这篇关于公开只读但具有私有设置程序的Objective-C属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!