我试图使用下面的代码从我的 NSDictionary 中删除一个数组(从我的表视图中删除一个对象),但是由于某种原因我在这一行崩溃了:
[self.friendData removeObjectAtIndex:indexPath.row];
出现此错误:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSSingleObjectArrayI removeObjectAtIndex:]:无法识别的选择器发送到实例 0x1c0618e20”
知道这是为什么以及解决方法是什么吗?请参阅 self.friendData 的控制台信息和下面的代码:
视图控制器.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;
Run Code Online (Sandbox Code Playgroud)
视图控制器.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!");
}];
}
}
}
Run Code Online (Sandbox Code Playgroud)
控制台 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;
}
Run Code Online (Sandbox Code Playgroud)
我不知道最后一条评论发生了什么,但这就是我所看到的问题的根源。
当您从 AFNetworking(或基本上任何网络框架)接收数据时,您会得到NSArray *,而不是NSMutableArray *。
当您使用时,NSMutableArray *mutArray = (NSMutableArray *) someArray;您只是强制转换该元素,但它仍然是一个NSArray *. 这就是它崩溃的原因。
你应该使用NSMutableArray *mutArray = [someArray mutableCopy];它来创建一个实际的 NSMutableArray并将其分配给变量。
| 归档时间: |
|
| 查看次数: |
2526 次 |
| 最近记录: |