
Untitled
By: a guest on
Aug 10th, 2012 | syntax:
None | size: 0.82 KB | hits: 6 | expires: Never
decodeObjectWithKey: results in high retain counts
@interface Car : NSObject <NSCoding>
@property (nonatomic, retain) NSString * carName;
@property (nonatomic, retain) UIColor *color;
@end
@implementation Car
@synthesize carName, color;
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
NSLog(@"%d", color.retainCount); // prints 0
carName = [[aDecoder decodeObjectForKey:@"carName"] retain];
color = [[aDecoder decodeObjectForKey:@"color"] retain];
NSLog(@"%d", color.retainCount); // prints 4
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:carName forKey:@"carName"];
[aCoder encodeObject:color forKey:@"color"];
}
-(void) dealloc {
[carName release];
[color release];
[super dealloc];
}
@end