【问题标题】:Returning 'self' while it is not set to the result of '[(super or self) init...]'返回 '​​self' 而它未设置为 '[(super or self) init...]' 的结果
【发布时间】:2013-10-03 16:11:58
【问题描述】:

有人知道为什么 Xcode 5 Analyze 抱怨这个:

ZIFollowerRequestsCell.m:34:5: 在未设置为时返回“self” '[(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

感谢您的帮助。

【问题讨论】:

  • 这个类是否继承自其他类?
  • 它抱怨是因为你应该将[super init]的返回值分配给self
  • @RamyAlZuhouri 是的,它是一个 UITableViewCell 对象。
  • @H2CO3 是的,但我不知道为什么它不加载笔尖,所以我尝试使用 -loadNibNamed: 获取它。

标签: objective-c cocoa-touch memory-leaks clang-static-analyzer


【解决方案1】:

您正在分配 self 两次。第二次,嗯,Xcode 所说的。此外,最好不要在构造函数中使用self.variable,只需使用_variable

您注册了您的手机吗?假设您有一个 ZIFollowerRequestsCell.xib,其根视图是一个具有 ZIFollowerRequestsCell 类的单元格,请在您的视图控制器中尝试:

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; 
}

【讨论】:

    猜你喜欢
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 2012-11-28
    相关资源
    最近更新 更多