公开只读但具有私有 setter 的 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
;有什么方法可以强制使用仅在类中可用的合成 setter?
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
有关更多信息,请查看 Apple 的 文档
For more check out Apple's documentation
这篇关于公开只读但具有私有 setter 的 Objective-C 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!