2012-09-14 34 views
4

如何做到这一点:公开API只读,但内部读/写?

@interface Foo : NSObject 
@property (readonly) int bar; 
@end 

... 

@interface Foo() 
@property (nonatomic) int bar; 
@end 

@implementation Foo 
@synthesize bar = _bar; 
    // stuff that does self.bar = 123; 
@end 

使一些外部类不能调用foo.bar = 123 ..但里面的Foo的内部方法可以...?

回答

4

在类扩展添加到readwrite应该nonatomic做到这一点:

@property (readwrite, nonatomic) int bar;