我在2012年中期开始使用Objective-C语言进行编程,当时ARC取代MRC成为常规实践,使得后者几乎无需学习。
现在我正在尝试了解一些MRC的基础知识,以加深我对Objective-C内存管理的理解。
我现在感兴趣的是如何手动为声明的属性写入getter/setter。
到目前为止,我找到的唯一合理的例子来自苹果公司的"高级内存管理编程指南":
@interface Counter : NSObject {
NSNumber *_count;
}
@property (nonatomic, retain) NSNumber *count;
@end;
- (NSNumber *)count {
return _count;
}
- (void)setCount:(NSNumber *)newCount {
[newCount retain];
[_count release];
_count = newCount;
}
我的猜测是,要对 (nonatomic, copy) 进行相同的操作,我应该写出类似以下的代码:
- (NSNumber *)count {
return _count;
}
- (void)setCount:(NSNumber *)newCount {
[_count release];
_count = [newCount copy];
}
所以问题在于其余的组合,我不确定:
如果使用手动引用计数(MRC),我希望有人能够示范如何编写以下@property声明的显式getter / setter的示例:
1. @property (nonatomic, retain) NSNumber *count;
2. @property (nonatomic, copy) NSNumber *count;
3. @property (atomic, retain) NSNumber *count;
4. @property (assign) NSNumber *count;
5. What else is used often under MRC? (please share, if any other combinations exist)