仅在mutator在Objective-C Properties中保持私有时才显示访问器

时间:2011-03-10 10:57:32

标签: objective-c properties encapsulation accessor mutators

假设我有一个带有一些实例变量的类,我想通过Dot Notation属性公开那些“只读”使用的i-vars,但同时能够在我的类中使用Property Mutator ,还有Dot Notation。

问题是,我正在我的.h文件中声明:

@property (readonly) ....

然后,即使我自己编写mutator(-(void) setMyIvar:),也不能使用点符号,因为编译器正在抱怨(理所当然)该属性是只读的。

我尝试在我的.m文件中重新声明属性(在接口扩展内):

@inteface MyClass()
@property (retain) myIvar;
@end

但编译器不接受这种双重声明。

我有什么方法可以做到吗?

1 个答案:

答案 0 :(得分:2)

你可以这样做,但你必须这样做:

@property (retain,readonly) ....

在您的实施中:

@inteface MyClass()
@property (retain,readwrite) myIvar;
@end

换句话说,除了可访问性之外,声明应该是相同的。