【问题标题】:NSMutable array showing : -[__NSDictionaryM removeObjectAtIndex:]: unrecognized selector sent to instance [duplicate]NSMutable 数组显示:-[__NSDictionaryM removeObjectAtIndex:]:无法识别的选择器发送到实例 [重复]
【发布时间】: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


【解决方案1】:

这是错误的原因。

[orderDetails[cellIndexPath.section] removeObjectAtIndex:cellIndexPath.row];

orderDetails 是一个数组,而 [orderDetails[cellIndexPath.section] 不是。

orderDetails[x] 是从 JSON 中提取的对象,在本例中是 Object 的 Dictionary 类型。

【讨论】:

  • 我把你提到的那一行改成了:[orderDetails removeObjectAtIndex:cellIndexPath.row]; ..但是应用程序在它旁边在线崩溃。并显示 lldb 。虽然我可以看到该行被删除:-)
猜你喜欢
  • 2019-05-10
  • 1970-01-01
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
  • 2015-03-03
  • 1970-01-01
相关资源
最近更新 更多