Search code examples
objective-ccocoa-touchmemory-leaksclang-static-analyzer

Returning 'self' while it is not set to the result of '[(super or self) init...]'


Somebody knows why Xcode 5 Analyze complains about this:

ZIFollowerRequestsCell.m:34:5: Returning 'self' while it is not set to the result of '[(super or self) init...]'

#import "ZIFollowerRequestsCell.h"

@implementation ZIFollowerRequestsCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"ZIFollowerRequestsCell" owner:self options:nil];
        self = [nibArray objectAtIndex:0];

        self.profileImageView.image = nil;
        self.profileImageView.userInteractionEnabled = YES;
   }
   return self;
}

@end

@interface ZIFollowerRequestsCell : UITableViewCell <UIGestureRecognizerDelegate>
@end

Thanks for any help.


Solution

  • You are assigning self twice. The second time, well, what Xcode said. Also, it's good practice not to use self.variable in the constructor, just use _variable.

    Did you register your cell? Assuming you have a ZIFollowerRequestsCell.xib whose root view is a cell with class ZIFollowerRequestsCell, try this in your view controller:

    NSString * const REUSE_ID_CELL = @"ZIFollowerRequestsCell";
    
    - (void)registerNIBs
    {
        NSBundle *classBundle = [NSBundle bundleForClass:[ZIFollowerRequestsCell class]];
        UINib *nib = [UINib nibWithNibName:REUSE_ID_CELL bundle:classBundle];
        [[self tableView] registerNib:topNib forCellReuseIdentifier:REUSE_ID_CELL];
    }
    
    - (NSString *)reuseIdentifierForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return REUSE_ID_CELL; 
    }
    
    - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSString *reuseID = [self reuseIdentifierForRowAtIndexPath:indexPath];
        UITableViewCell *cell = [[self tableView] dequeueReusableCellWithIdentifier:reuseID];
        return cell; 
    }