Objective-C :(私有/公共属性)为外部类调用设置只读属性,为自身调用设置只读属性 [英] Objective-C : (private / public properties) making a property readonly for outside class calls and readwrite for self calls
问题描述
您是否知道一种方法可以使外部调用的属性只读,内部调用的属性可读写?
Would you know a way to make a property readonly for outside calls and readwrite for inside calls ?
我以前读过一些类似的东西
I've read times ago somthing that seemed like
在.h
@property(nonatomic, readonly) NSDate* theDate;
在.m
@interface TheClassName()
@property(nonatomic, retain) NSDate* theDate;
@end
但这会在编译 .m 时引发警告TheClassName 类延续中的属性 theDate 属性与类 TheClassName 属性不匹配".
but this raises a warning when compiling the .m "Property theDate attribute in TheClassName class continuation does not match class TheClassName property".
无论如何,它似乎有效(可以从课外读取但不能设置,可以从课内进行)但我应该错过一些东西以避免警告.或者如果你知道一个更好的方法来做到这一点......
Anyway, it seems to work (can read but not set from outside the class, can do both from inside) but I should have missed somehting to avoid the warning. Or if you know a better way to do this...
推荐答案
在你的 .h 中:
@property(nonatomic, retain, readonly) NSDate* theDate;
在您的 .m 中:
@interface TheClassName()
@property(nonatomic, retain, readwrite) NSDate* theDate;
@end
这篇关于Objective-C :(私有/公共属性)为外部类调用设置只读属性,为自身调用设置只读属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!