【发布时间】:2011-12-19 19:50:09
【问题描述】:
在 ARC 之前,如果我希望某个属性在使用时是只读的,但在类中可写,我可以这样做:
// Public declaration
@interface SomeClass : NSObject
@property (nonatomic, retain, readonly) NSString *myProperty;
@end
// private interface declaration
@interface SomeClass()
- (void)setMyProperty:(NSString *)newValue;
@end
@implementation SomeClass
- (void)setMyProperty:(NSString *)newValue
{
if (myProperty != newValue) {
[myProperty release];
myProperty = [newValue retain];
}
}
- (void)doSomethingPrivate
{
[self setMyProperty:@"some value that only this class can set"];
}
@end
对于 ARC,如果我想覆盖 setMyProperty,您不能再使用保留/释放关键字,这样是否足够且正确?
// interface declaration:
@property (nonatomic, strong, readonly) NSString *myProperty;
// Setter override
- (void)setMyProperty:(NSString *)newValue
{
if (myProperty != newValue) {
myProperty = newValue;
}
}
【问题讨论】:
标签: iphone objective-c ios automatic-ref-counting