- (id)init{
if(![super init]){
return nil;
}
_rssItems = [[NSMutableArray alloc]init];
return self;
}
If I analyze my project I get this warning:
Instance variable used while ‘self’ is not set to the result of
‘[(super or self) init…]’
What do I have to change?
Thanks!
Nowadays, Apple’s official recommendation is:
The idea is that
init(in particular, in this case,[super init]) is allowed to return an object other than self, and the expected behavior in that case is to work with that object instead–the easiest way to deal with this is to just setselfto whateversuperreturns. Also note thatreturn self;works fine whetherselfis nil or not, hence the reversal of yourif.Of course, most classes don’t perform this switching trick–but this is good practice anyway because Apple expects everyone to be using this exact pattern for their
init, so even if your subclass currently works without assigning toself, they could easily change that behavior in the future without warning.