Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Whats wrong with this code under ARC? I get above error:

- (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;
}
like image 430
Christian Loncle Avatar asked Dec 17 '11 22:12

Christian Loncle


1 Answers

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

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

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

like image 85
tacos_tacos_tacos Avatar answered Oct 23 '22 12:10

tacos_tacos_tacos