不应该是僵尸的僵尸对象

时间:2012-06-07 18:11:44

标签: iphone objective-c xcode release-management nszombie

我有一个导航视图iPhone应用程序。我创建了一个简单的对象,它具有'name'NSString和'weight'NSNumber。这个应用程序在加载单元格时不断崩溃。这是方法:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
factor *toAdd = [factors objectAtIndex:indexPath.row];

cell.textLabel.text = toAdd.name;
cell.detailTextLabel.text = [toAdd.weight stringValue];
    // ^ crashes here...
    // stringByAppendingString:@"%"];

return cell;
}

在NSNumber上调用stringValue方法时,我在控制台上收到“发送到deallocated instance的消息”。我不明白为什么会这样。上面的行没有问题访问该名称,我没有[发布]声明。

谢谢

编辑: 这是我的因素的init方法。我仔细检查并且重量是(保留,非原子)并在实现中合成,就像名字一样。

- (id) init{
if( self = [super init] )
{
    weight = [NSNumber numberWithInt:10];
    name = @"Homework";
}
return self;
}

2 个答案:

答案 0 :(得分:1)

您没有在init中使用属性setter方法。因此,不保留对象。

试试这个:

- (id) init{
if( self = [super init] )
{
    self.weight = [NSNumber numberWithInt:10];
    self.name = @"Homework";
}
return self;
}

要避免这些类型的错误,您可以使用以下方法合成属性:

@synthesize name = _name;

答案 1 :(得分:0)

能够成功访问name属性与weight属性是否尚未发布无关。所有这些都告诉你,你的factor还活着,并且它的name也很活跃。我猜你没有在weight的实施中正确保留factor财产。

编辑:添加了代码,肯定你在做什么。

相关问题