1

I am trying to encode some data state in a UITableViewController. In the first time, I init the object with Nibname without any problem. However, when I initWithCoder, the UITableViewController still loads but when I clicked on a cell, the application crash and the debugger tells me about EXEC_BAD_ACCESS, something wrong with my memory, but I do not know

Here is my code:

- (id) init {
    if(self = [self initWithNibName:@"DateTableViewController" bundle:nil]) {        
        self.dataArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
    }
    return self;
}

// 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    int index = indexPath.row;
 cell.textLabel.text = [self.dataArray objectAtIndex:index];;
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    return @"Test Archiver";
}
- (void)encodeWithCoder:(NSCoder *)coder {
    [super encodeWithCoder:coder];
    [coder encodeObject:self.dataArray];
}
- (id)initWithCoder:(NSCoder *)coder {
 return [self init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    int index = indexPath.row;
    [self.dataArray addObject:[NSString stringWithFormat:@"Hello %d", index]];
    [self.tableView reloadData];
}

2 Answers 2

1

I see two problems here:

1) You are serializing your dataArray using encodeObject: but omit to deserialize it in initWithCoder:

2) You should rather use NSKeyedArchiver's method encodeObject:forKey:. Then you are free to use the serialized information in the decoding or not.

Other than that I can only say to answer your question it would be helpful to include information about where the crash happens.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. I will try with deserialize first. But about the crash, as I already mentioned: However, when I initWithCoder, the UITableViewController still loads properly but when I clicked on a cell, the application crash and the debugger tells me about EXEC_BAD_ACCESS, something wrong with my memory, but I do not know
could you post ur new code using these recommendations that worked? Thanks
1

There is only one memory problem in this code: In init you are leaking the NSMutableArray. But that’s kind of the opposite problem you are looking for. The bug you described is not in the posted code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.