ret*_*dev 2 encapsulation properties objective-c ios
我看到了这个帖子,但是在iOS7中并没有完全清楚,因为编程指南说你可以省略@synthesise属性的关键字.
我想有一个@property是readonly外部,而是readwrite内部.我可以只使用这样的@property关键字吗?
编辑:提供的答案将被认为更正确,或者至少更为惯用.要readwrite在类扩展中拥有单独的属性,还是直接在实现中访问ivar?
编辑:显示我正在使用类扩展,如答案中所示.
//.h
@interface CACustomerAuthenticator : NSObject
@property (nonatomic, copy, readonly) NSString *username;
@end
//.m
@interface CACustomerAuthenticator ()
@property (nonatomic, copy, readwrite) NSString *username;
@end
Run Code Online (Sandbox Code Playgroud)
如果要执行此操作,请在类扩展中重新声明属性.
例如,如果您有一个名为MyClass的类:
MyClass.h
@property (nonatomic, copy, readonly) NSString *username;
Run Code Online (Sandbox Code Playgroud)
MyClass.m
// Create a class extension before the @implementation section
@interface MyClass ()
@property (nonatomic, copy, readwrite) NSString *username;
@end
Run Code Online (Sandbox Code Playgroud)
现在公共接口的属性为readonly,私有接口将其作为readwrite.并且您不需要@synthesize属性,_username默认情况下支持iVar .
| 归档时间: |
|
| 查看次数: |
2183 次 |
| 最近记录: |