I have 2 questions on how to make the readonly property correct in Objective-C 2.0 +.
Here is my original approach, call him solution 1 :
@interface ClassA{
@private
NSMutableArray *a_;
}
@property (nonatomic, readonly) NSMutableArray *a;
@end
@implementation ClassA
@synthesize a = a_;
- (NSMutableArray *)a{
if(nil == a_){
a_ = [[NSMutableArray alloc] array];
}
return a_;
}
- (void)dealloc{
[a_ release];
[super dealloc];
@end
When I compile and analyze it, the system reports this warning: "potential leak in" return a _ ".
Then I read the Objective-C document again and found a different approach, as shown below. Call it solution 2 .
@interface ClassB{
@private
NSMutableArray *a_;
}
@property (nonatomic, readonly, retain) NSMutableArray *a;
@end
@interface ClassB ()
@property (nonatomic, readwrite, retain) NSMutableArray *a;
@end
@implementation ClassB
@synthesize a = a_;
- (id)init{
if(self = [super init]){
self.a = [NSMutableArray array];
}
return self;
}
- (void)dealloc{
self.a = nil;
[super dealloc];
@end
Now here are my questions:
- Is it possible to use solution 1 and get rid of the "potential leak"?
- Does decision 2 solve the general solution?
Thanks guys!
- Ton
source
share