Search code examples
iphonensindexpath

Why does NSIndexPath does not work fine in iOS5?


Where ever I used indexpath values, the app quits in iOS5. Can anyone say the exact reason for it? I red lot of questions & answers related to this. But still in a confusion.

What is the reason behind this?

What are all the changes that we should do to prevent crashing?

Where can I know these type of code-changes for the upcoming iOS versions?

Update:

Here is the crashed code:

MyClass.h

@interface MyClass:UIViewController <......> {
...
NSIndexPath* indexPathOfCell;
...
}

...
...


MyClass.m

....

- (IBAction) someAction: (UIButton *) buttonName {
...
indexPathOfCell = [MyTableView indexPathForCell:swipeCell]; //swipeCell is a UITableViewCell
...
}

...

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
...
[self deleteEntity: indexPathOfCell];
...
}

...
...

- (void)dealloc
{
[indexPathOfCell release];
indexPathOfCell = nil;
...
}

Solution

  • In the someAction method, you are assigning NSIndexPath object to indexPathOfCell member. But since it is autoreleased, it will not be available in another method. So you are getting the crash. Instead, do as the following, where I suggest you to retain the variable.

    - (IBAction) someAction: (UIButton *) buttonName {
      ...
      indexPathOfCell = [[MyTableView indexPathForCell:swipeCell] retain]; //swipeCell is a UITableViewCell
      ...
    }