I've started programming on Objective-C language in the middle of 2012 in the time when ARC replaced MRC as a general practice making the latter almost unnecessary to learn.
Now I am trying to understand some basics of MRC to deepen my knowledge of Memory Management in Objective-C.
The thing I am interested in now, is how to write getters/setters
for declared @properties
explicitly, by hands.
By this time the only sane example I found is from "Advanced Memory Management Programming Guide" by Apple:
@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;
}
My guess is that to make the same for (nonatomic, copy) I should write something like:
- (NSNumber *)count {
return _count;
}
- (void)setCount:(NSNumber *)newCount {
[_count release];
_count = [newCount copy];
}
So the question is about the rest of combinations I am not sure about:
I would thankful for someone who could show me examples of how explicit getters/setters should be written for the following @property declarations given Manual Reference Counting (MRC) is used:
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)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…