2017-10-22 107 views
-2

我試圖刪除我的NSDictionary數組(刪除從我的tableview的對象)與下面的代碼,但由於某種原因,我得到這一行崩潰:的OBJ-C:錯誤__NSSingleObjectArrayI removeObjectAtIndex:]:無法識別的選擇發送到實例?

[self.friendData removeObjectAtIndex:indexPath.row];

與此錯誤:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSingleObjectArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 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; 
    } 
+0

我假設你有'@財產(強,非原子)的NSArray * friendData;'或等價的東西嗎?它必須是'NSMutableArray *'才能使用該方法。 'friendData'是一個字典數組嗎? –

+0

@AlejandroIvánfriendData是財產(強,非原子)的NSMutableArray。也就是說,我用這個相同的代碼爲另一個NSMutableArray,它運行得很好嗎? – Brittany

+0

你能編輯您的帖子粘貼整個相關的代碼?從我看到的情況來看,這更像是一個類型問題而不是代碼。你在做'self.friendData = @ [];'在哪裏? –

回答

1

我不知道用最後的評論發生了什麼,但是這是你的問題的根源,因爲我看到它。

當您從AFNetworking(或基本上任何網絡框架)收到數據時,您會收到NSArray *而不是NSMutableArray *

當你使用NSMutableArray *mutArray = (NSMutableArray *) someArray;你只是強制鑄造的元素,但它仍然是NSArray *。這就是它崩潰的原因。

您應該使用NSMutableArray *mutArray = [someArray mutableCopy];哪個將創建一個實際的NSMutableArray並將其分配給該變量。

相關問題