Search code examples
objective-cobjectself

The result of a delegate init call must be immediately returned or assigned to 'self'


In my project I have a class that declares following methods:

-(id)initWithSections:(NSUInteger)s rows:(NSUInteger)r
{
        if(self=[self init])
        {sections = [[NSMutableArray alloc] initWithCapacity:s];

            for (int i=0; i<=s; i++){
                NSMutableArray *rows = [NSMutableArray arrayWithCapacity:r];
                for (int j=0; j<=r; j++){
                    [rows insertObject:[NSNull null] atIndex:j];
                }
                [sections addObject:rows];
            }
        }

    return self;
}

I also one more method that calls the method above:

-(id)OneMoreMethod:(NSMutableArray *)obj1 :(NSMutableArray *)obj2
{
    NSInteger b = 3;

   [self initWithSections:b rows:b];//The result of a delegate init call must be immediately returned or assigned to 'self'

    return  self;
}

I got error:

The result of a delegate init call must be immediately returned or assigned to 'self'.

I don't see an error. The most interesting this is build success in other project.


Solution

  • Since initWithSections:rows: returns "self", it's asking from you to use that self. You're just ignoring the "self" returned from that method.

    It doesn't look good, and also it will probably cause another problems, but this will solve the original problem:

    self = [self initWithSections:b rows:b];
    
    return self;