【发布时间】:2015-03-04 05:47:09
【问题描述】:
我正在尝试从 NSMutableArray 中删除对象,但仍然出现上述错误。以下是我创建和使用数组的方式。
#import "CartViewController.h"
@interface CartViewController ()
{
NSMutableArray *orderDetails; //my Array
}
@end
-(void)getCart{
SuccessBlock successBlock = ^(NSData *response){
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
orderDetails = [[NSMutableArray alloc]init];
orderDetails = [responseDict objectForKey:@"orderDetails"];
[self.cartTableView reloadData];
};
FailureBlock failureBlock = ^(NSError *error){
NSLog(@"Failure Block %@",[error localizedDescription]);
};
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@00",BASE_URL,GET_CART]];
HTTPPostRequest *postReq = [[HTTPPostRequest alloc]initWithURL:url successBlock:successBlock failureBlock:failureBlock];
[postReq startRequest];
}
现在我正在使用SWTableViewCell,我想从中删除一个特定的单元格,从而从数组中删除一个条目。
#pragma mark - SWTableViewDelegate
- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index
{
switch (index) {
case 0:
{
NSLog(@"left button 0 was pressed");
[cell setLeftUtilityButtons:[self leftUndoButton]WithButtonWidth:self.cartTableView.frame.size.width];
[cell showLeftUtilityButtonsAnimated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self deleteCell:cell];
});
break;
}
default:
{
break;
}
}
}
-(void)deleteCell:(SWTableViewCell *)cell{
NSIndexPath *cellIndexPath = [self.cartTableView indexPathForCell:cell];
[orderDetails[cellIndexPath.section] removeObjectAtIndex:cellIndexPath.row];//Exception breakpoint stops here by showing : [__NSDictionaryM removeObjectAtIndex:]: unrecognized selector sent to instance 0x7968afd0
[self.cartTableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
【问题讨论】:
-
在
orderDetails = [responseDict objectForKey:@"orderDetails"];之后立即记录orderDetails的值,看看它说了什么。如果你接下来要做的是替换它,你也不需要分配/初始化数组。 -
首先要检查的是,orderDetails 的代码是什么?如果您在那里添加断点或启用异常断点,您可以询问调试器 orderDetails 的类是什么。 (如果你需要,很高兴展示如何)。该错误表明无论它是什么都不会响应您调用的方法。由于这绝对是可变数组上的一种方法,因此它必然是一个错误指向或 nil 指针。
标签: ios objective-c nsmutablearray