Objective-C:(私有/公共属性)只读属性为外部类调用和readwrite的自调用 [英] Objective-C : (private / public properties) making a property readonly for outside class calls and readwrite for self calls

查看:401
本文介绍了Objective-C:(私有/公共属性)只读属性为外部类调用和readwrite的自调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您会知道一种为外部调用只读属性的方法和为内部调用读写的方法吗?

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时产生警告属性theDate属性TheClassName类继承不匹配类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:(私有/公共属性)只读属性为外部类调用和readwrite的自调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆