Question

I have a navigation view iPhone app. I created a simple object that has a 'name' NSString and a 'weight' NSNumber. This app keeps crashing when loading a cell. Here is the method:

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

I get the "message sent to deallocated instance" on the console when calling the stringValue method on the NSNumber. I don't understand why this is happening. The line above has no problem accessing the name and I have no [release] statements.

Thank you

EDIT: Here's my factor's init method. I double checked and weight is (retain,nonatomic) and synthesized in implementation, just like name.

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

Solution

You are not using the property setter method in your init. Thus the objects are not retained.

Try this:

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

To avoid these kinds of errors you can synthesize properties with:

@synthesize name = _name;

OTHER TIPS

Being able to access the name property successfully has nothing to do with whether or not the weight property has been released yet. All that tells you is that your factor is alive and well, and that its name is also alive and well. I'm guessing you're not properly retaining your weight property in the implementation of factor.

EDIT: With the added code, that's definitely what you're doing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top