我正在使用NSMutableArray
并尝试从NSMutableArray
中删除对象,然后崩溃
[[self.sectionsArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
.h
就像
NSMutableArray *sectionsArray;
@property (nonatomic, retain) NSMutableArray *sectionsArray;
.m is like:
@synthesize sectionsArray;
#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Perform segue to candy detail
NSLog(@"section: %d row: %d", indexPath.section, indexPath.row);
NSLog(@"section :%@", [[self.sectionsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]);
[[self.sectionsArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
// [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
崩溃日志就像:
2014-01-28 11:56:24.989 CandySearch[828:c07] section: 2 row: 0
2014-01-28 11:56:24.991 CandySearch[828:c07] section :<Candy: 0x75a5a70>
2014-01-28 11:56:24.992 CandySearch[828:c07] -[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x75d8c90
2014-01-28 11:56:24.992 CandySearch[828:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x75d8c90'
*** First throw call stack:
(0x1c9b012 0x10d8e7e 0x1d264bd 0x1c8abbc 0x1c8a94e 0x370d 0xcc285 0xcc4ed 0xad65b3 0x1c5a376 0x1c59e06 0x1c41a82 0x1c40f44 0x1c40e1b 0x1bf57e3 0x1bf5668 0x1cffc 0x1d52 0x1cc5 0x1)
libc++abi.dylib: terminate called throwing an exception
我可以看到该索引处的对象。但它仍在崩溃 为什么呢?
答案 0 :(得分:4)
位于索引“indexPath.section”的数组是NSArray对象,而不是NSMutableArray 只需阅读崩溃日志: NSArrayI removeObjectAtIndex:
请检查sectionsArray的对象
答案 1 :(得分:0)
您想在不可变数组(NSArray)中调用removeObjectAtIndex:
。这就是为什么,它会导致崩溃。 (那个,section数组是可变的,但它有不可变数组对象的数组。)
答案 2 :(得分:0)
我的可变数组对象也因为被分配了NSArray而导致崩溃。所以,我通过使用
解决了这个问题[NSMutableArray arrayWithArray:]