objective-c ARC readonly properties and private setter implementation

青春壹個敷衍的年華 提交于 2019-12-02 18:03:38

Yes, that is adequate, but you don't even need that much.

You can do

- (void)setMyProperty:(NSString *)newValue
{ 
      myProperty = newValue;
}

The compiler will do the right thing here.

The other thing though, is you don't even need THAT. In your class extension you can actually respecify @property declarations.

@interface SomeClass : NSObject
@property (nonatomic, readonly, strong) NSString *myProperty;
@end

@interface SomeClass()
@property (nonatomic, readwrite, strong) NSString *myProperty;
@end

Doing that, you just need to synthesize and you have a private setter that is synthesized for you.

You can redeclare your property as readwrite in interface extension:

@interface SomeClass()
@property (nonatomic, strong, readwrite) NSString *myProperty;
@end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!