The instance variable used while "I" is not set as a result of "[(super or self) init ...]

- (id)init{ if(![super init]){ return nil; } _rssItems = [[NSMutableArray alloc]init]; return self; } 

If I analyze my project, I get the following warning:

The instance variable used while "self" is not set to the result of '[(super or me) init ...]'

What do i need to change?

Thanks!

+7
source share
1 answer

Currently, Apple's official recommendation:

 - (id)init{ self = [super init]; if(self){ _rssItems = [[NSMutableArray alloc]init]; } return self; } 

The idea is that init (specifically, in this case [super init] ) is allowed to return an object other than self, and the expected behavior in this case is to work with this object instead - the easiest way to deal with this is just to set self to what super returns. Also note that return self; works great, regardless of whether self zero or not, hence the inversion of your if .

Of course, most classes do not perform this switching trick, but this is good practice, since Apple expects everyone to use this exact template for their init , so even if your subclass is currently working without self assignment, they could easily change that future behavior without warning.

+17
source

All Articles