Obj-C:错误__NSSingleObjectArrayI removeObjectAtIndex:]:发送到实例的无法识别的选择器?

时间:2017-10-22 00:14:10

标签: ios objective-c

我尝试使用下面的代码从我的NSDictionary中删除一个数组(从我的tableview中删除一个对象),但是出于某种原因我在这一行遇到了崩溃:

  

[self.friendData removeObjectAtIndex:indexPath.row];

出现此错误:

  

由于未捕获的异常而终止应用   ' NSInvalidArgumentException',原因:' - [__ NSSingleObjectArrayI   removeObjectAtIndex:]:发送到实例的无法识别的选择器   0x1c0618e20'

知道这是为什么以及解决方案是什么?请参阅下面的self.friendData和代码的控制台信息:

ViewController.h

@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (weak, nonatomic) IBOutlet UITableView *sidetableView;
@property (strong, nonatomic) NSMutableArray *myFriendData;
@property (weak, nonatomic) IBOutlet UITableView *neighboursView;
@property (strong, nonatomic) NSMutableArray *friendData;
@property (strong, nonatomic) NSMutableArray *neighbourData;

@property (strong, nonatomic) IBOutlet UITableView *friendsView;

ViewController.m

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.sidetableView) {
        return UITableViewCellEditingStyleDelete;
    }

    if (tableView == self.friendsView) {
        return UITableViewCellEditingStyleDelete;
    }

    else {
        return UITableViewCellEditingStyleNone;
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        if (tableView == self.friendsView) {

            NSMutableDictionary *nodeData = [[self.friendData objectAtIndex:indexPath.row] mutableCopy];

            NSString *nid = [nodeData objectForKey:@"nid"];
            [nodeData setObject:nid forKey:@"nid"];


            [self.friendData removeObjectAtIndex:indexPath.row];

            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];



            [DIOSNode nodeDelete:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSLog(@"node deleted!");
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"could not delete node!");
            }];


        } else {

        NSMutableDictionary *nodeData = [[self.myFriendData objectAtIndex:indexPath.row] mutableCopy];

        NSString *nid = [nodeData objectForKey:@"nid"];
        [nodeData setObject:nid forKey:@"nid"];
        NSLog(@"%@",nid);

        [self.myFriendData removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];



        [DIOSNode nodeDelete:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"node deleted!");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"could not delete node!");
        }];



        }

    }
    }

控制台self.friendData:

This is the frienddata (
        {
        address2 = "street";
        body = "nfkkdkckmfkekxkx\n";
        childrenunder = No;
        city = "<null>";
        "emergency facility" = Yes;
        friendphoto = "files/stored/1504910599.jpg";
        nid = 1796;
        "node_title" = "josh";
        phone = 2369893091;
        "postal code" = V;
        "property type" = House;
        "special skills" = "med";
        "star rating" = 1;
        supervision = No;
        uid = 47;
        uid2 = 182;
    }

1 个答案:

答案 0 :(得分:1)

我不知道最后一条评论发生了什么,但这就是我看到的问题的根源。

当您从AFNetworking(或基本上任何网络框架)接收数据时,您会获得NSArray *,而不是NSMutableArray *

当你使用NSMutableArray *mutArray = (NSMutableArray *) someArray;时,你只是强制投射元素,但它仍然是NSArray *。这就是崩溃的原因。

您应该使用NSMutableArray *mutArray = [someArray mutableCopy]; 将创建一个实际的NSMutableArray 并将其分配给变量。