I am storing some Objective C based objects in to NSuserdefaults.
To retrieve data from NSuserDefaults, I use initWithCoder method.
I have seen two different implementations of this:
Implementation 1
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (self != nil){
//decode properties, other class vars
self.variable = [decoder decodeObjectForKey:@"variable"];
}
return self;
}
Implementation 2
- (id)initWithCoder:(NSCoder *)decoder {
self = [[CustomClass alloc] init];
if (self != nil){
//decode properties, other class vars
self.variable = [decoder decodeObjectForKey:@"variable"];
}
return self;
}
Which is the correct way?
What is the difference between these two?
This really isn't a difference in NSUserDefaults implementation, the difference whether or not your class is a subclass. Subclasses call [super init] to gain the properties of their superclasses (ex. 2), otherwise you can just alloc and init the custom class (ex. 1).