-2

I'm trying to remove an array from my NSDictionary (deleting an object from my tableview) with the code below, however for some reason I get a crash at this line:

[self.friendData removeObjectAtIndex:indexPath.row];

with this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSingleObjectArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x1c0618e20'

Any idea why this is and what the fix would be? See console info for self.friendData and code below:

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!");
        }];



        }

    }
    }

Console 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;
    }
Brittany
  • 1,359
  • 4
  • 24
  • 63
  • I assume you have `@property (strong, nonatomic) NSArray *friendData;` or something equivalent? It has to be `NSMutableArray *` in order to have that method available. Is `friendData` an array of dictionaries? – Alejandro Iván Oct 22 '17 at 00:16
  • @AlejandroIván friendData is property (strong, nonatomic) NSMutableArray. That said, I use this same code for another NSMutableArray, and it operates just fine? – Brittany Oct 22 '17 at 00:18
  • Could you edit your post to paste the whole relevant code? This is more like a type issue rather than a code one, from what I see. Are you doing `self.friendData = @[];` somewhere? – Alejandro Iván Oct 22 '17 at 00:19
  • @AlejandroIván See full code above - the portion of the code in which I remove an object from self.myFriendData works great - removing from self.friendData however causes the error. – Brittany Oct 22 '17 at 00:25
  • That looks OK. Where does `friendData` come from? Maybe that particular method returns `NSArray *` instead of `NSMutableArray *` and that's the source of your problem. Try to use `mutableCopy` when assigning it. – Alejandro Iván Oct 22 '17 at 00:28
  • @AlejandroIván Ahhhh using mutable copy did the trick - thank you! – Brittany Oct 22 '17 at 00:34

1 Answers1

5

I don't know what happened with the last comment, but that's the source of your problem as I see it.

When you receive data from AFNetworking (or basically whatever networking framework), you get NSArray *, not NSMutableArray *.

When you use NSMutableArray *mutArray = (NSMutableArray *) someArray; you're just force casting the element, but it's still a NSArray *. That's why it crashes.

You should rather use NSMutableArray *mutArray = [someArray mutableCopy]; which will create an actual NSMutableArray and assign it to the variable.

Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30