【问题标题】:iOS & Stackmob - [NSNull length]: unrecognized selector sent to instanceiOS 和 Stackmob - [NSNull 长度]:发送到实例的无法识别的选择器
【发布时间】: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'

提前致谢。

【问题讨论】:

标签: ios stackmob


【解决方案1】:

我认为这可能是因为您的 self.username 为空。请注意,如果您从 json 获取用户名数据,则不能使用 if(username){...} 但是

if(![username isKindOfClass:[NSNull class]])

避免空数据,因为 json 解释器会生成一个NSNull 对象。

【讨论】:

  • 虽然这个解决方案有效,但我在这里找到了更好的解决方案:stackoverflow.com/questions/16607960/…
  • @पवन 虽然这可能在某些情况下有效,但在生产中使用它是一种非常危险的策略。
  • 但是 JSON 和这个有什么关系呢?
猜你喜欢
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2021-12-29
  • 2011-08-23
  • 2023-03-09
相关资源
最近更新 更多