Initializer does not assign the result of the super initializer to self
ID |
objectivec.correctness.init_assign_self |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Api Misuse |
Language |
Objective-C |
Description
This initializer never assigns the result of another initializer to self. An initializer is allowed to return a different instance than the one it was sent to - or nil on failure - so its result must be assigned back to self before any ivar is set, and the rest of the body must run only when that result is non-nil. Start the body with self = [super init…]; if (self) { … }, or, in a secondary initializer, self = [self initWith…];.
Rationale
This initializer never assigns the result of another initializer to self. An initializer is allowed to return a different instance than the one it was sent to - or nil on failure - so its result must be assigned back to self before any ivar is set, and the rest of the body must run only when that result is non-nil. Start the body with self = [super init…]; if (self) { … }, or, in a secondary initializer, self = [self initWith…];.
The following code illustrates the pattern detected by this rule:
@implementation NoSuperCall
// FLAGGED: Initializer does not assign the result of the super initializer to self
- (id)init {
_name = @"anonymous";
return self;
}
@end