【发布时间】:2019-07-16 20:31:37
【问题描述】:
我正在尝试使用新数据(self.filtered,一个 NSMutableArray)重新加载我的 tableView。为了做到这一点,我的理解是我需要从原始 self.filtered 中删除所有对象,然后用更新的 NSMutableArray 重新填充它。但是,由于某种原因,当我运行以下命令时,出现以下崩溃错误(是的,所有值都按应有的方式填充......例如,没有空响应):
由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[__NSArrayI removeAllObjects]: 无法识别的选择器发送到实例 0x28009c540'
Viewcontroller.h
@property (nonatomic, strong) NSMutableArray *filtered;
ViewController.m
-(void)viewDidLoad {
NSString *myID = [session user][@"user"][@"uid"];
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"uid ==[cd] %@", targetedUser];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"targetuser ==[cd] %@", myID];
NSPredicate *p3 = [NSPredicate predicateWithFormat:@"uid ==[cd] %@", myID];
NSPredicate *p4 = [NSPredicate predicateWithFormat:@"targetuser ==[cd] %@", targetedUser];
NSPredicate *p5 = [NSPredicate predicateWithFormat:@"targetuser ==[cd] %@", universal];
NSCompoundPredicate *pIntermediary1 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2]];
NSCompoundPredicate * pIntermediary2 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p3, p4]];
NSCompoundPredicate * pIntermediary3 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p5]];
NSCompoundPredicate *pFinal = [NSCompoundPredicate orPredicateWithSubpredicates:@[pIntermediary1, pIntermediary2, pIntermediary3]];
self.filtered = [self.messages filteredArrayUsingPredicate:pFinal];
}
- (IBAction)sendReply:(id)sender {
NSMutableArray *aArr = [NSMutableArray arrayWithObjects:self.filtered,nil];
NSLog(@"BEFORE IT %@", aArr);
[aArr addObject:nodeData];
[self.filtered removeAllObjects];
[self.filtered addObject:aArr];
[self.tableView reloadData];
NSLog(@"LASTLY %@", self.filtered);
}
此方法中没有 NSArrays - 只有 NSMutableArrays,所以我不确定为什么会这样?
【问题讨论】:
-
您的
filtered属性包含不可变数组,而不是可变数组,因此会出现错误。展示您如何声明该属性并展示您如何为其赋值。 -
BTW -
[self.filtered addObject:aArr];行将整个数组添加到数组中。它不会将aArr的对象直接添加到filtered。换句话说,你留下了一个数组数组。这就是你想要的吗? -
仔细查看您的错误消息,发现您正在尝试调用
removeAllObjects来对抗NSArray,而不是您所期望的NSMutableArray。 -
@rmaddy 查看更新
-
啊@CodeBender 见上面的编辑。那么在 viewDidLoad 结束时,我的 self.filtered 变得不可变了吗?我怎样才能让它再次可变?
标签: ios objective-c