- (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:
- (id)init{
self = [super init];
if(self){
_rssItems = [[NSMutableArray alloc]init];
}
return self;
}
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 set self
to whatever super
returns. Also note that return self;
works fine whether self
is nil or not, hence the reversal of your if
.
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 to self
, they could easily change that behavior in the future without warning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With