【发布时间】:2023-04-09 00:16:01
【问题描述】:
在我的 iOS 应用程序中,我试图更新数据库中的用户信息(使用 Stackmob),但我不断收到“无法识别的选择器发送到实例”。
- (IBAction)save:(UIButton *)sender {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"User"];
NSPredicate *predicte = [NSPredicate predicateWithFormat:@"username == %@", self.username];
[fetchRequest setPredicate:predicte];
[self.managedObjectContext executeFetchRequest:fetchRequest onSuccess:^(NSArray *results) {
NSManagedObject *todoObject = [results objectAtIndex:0];
[todoObject setValue:@"example@gmail.com" forKey:@"email"];
[self.managedObjectContext saveOnSuccess:^{
NSLog(@"You updated the todo object!");
} onFailure:^(NSError *error) {
NSLog(@"There was an error! %@", error);
}];
} onFailure:^(NSError *error) {
NSLog(@"Error fetching: %@", error);
}];
}
这是我得到的完整错误:
-[NSNull length]: unrecognized selector sent to instance 0x1ec5678
2013-07-21 12:01:25.773 [29207:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[NSNull length]: unrecognized selector sent to instance 0x1ec5678'
提前致谢。
【问题讨论】:
-
有些地方你得到 Nil 值或对象并且你试图设置 nil 这就是你得到这个错误的原因。检查 setValue 是否为 nil。
-
你能打印 self.username 看看它包含什么吗?