ARC error: init methods must return a type related to the type of the receiver [4]

What is wrong with this code in ARC? I get the error above:

- (Moment *)initMoment:(BOOL)insert { if (insert) { self.moment = [NSEntityDescription insertNewObjectForEntityForName:@"Moment" inManagedObjectContext:self.managedObjectContext]; } else { self.moment = [NSEntityDescription insertNewObjectForEntityForName:@"Moment" inManagedObjectContext:nil]; } return self.moment; } 
+8
initialization ios automatic-ref-counting
source share
1 answer

The init method that was posted in the question was incorrect. The init method should (usually) take the form:

 -(id)initWithParams:(BOOL)aBoolParam { if (self = [super init]) { //do stuff } return self; } 

The problem with the code above was that it was done as a class method, so if the poster wanted to do this, it had to do moment = [[Moment alloc] init] and return it.

+9
source share

All Articles