【问题标题】:-[__NSDictionaryM intValue]: unrecognized selector sent to instance 0x13f26a60-[__NSDictionaryM intValue]:无法识别的选择器发送到实例 0x13f26a60
【发布时间】:2013-03-04 14:54:38
【问题描述】:

我正在尝试获取核心数据库的记录,但它给了我以下错误。

错误 -[__NSDictionaryM intValue]:无法识别的选择器发送到实例 0x13f26a60

上线

NSArray *results = [moc executeFetchRequest:request error:&error];

这里是获取请求的完整代码。

NSManagedObjectContext *moc = [delegate managedObjectContext];


NSPredicate *predicate =[NSPredicate predicateWithFormat:@"NOT (serverId IN %@)", a];
[request setEntity:[NSEntityDescription entityForName:@"Categories" inManagedObjectContext:moc]];
[request setPredicate:predicate];

NSError *error = nil;
id results = [moc executeFetchRequest:request error:&error];
NSLog(@"results %@",results);
// error handling code
[request release];

【问题讨论】:

  • 我在您的代码中的任何地方都没有看到对 intValue 的调用。
  • 在运行时,有时对象会被转换为其他类型,让您猜测。这也发生在这里。
  • 如果serverId是int值,那么你可能需要将NOT (serverId IN %@)改为NOT (serverId IN %d)
  • a 是 NSArray 吗?它包含什么?
  • serverId 是整数,当我更改为 NOT (serverId IN %d) 然后它给出以下错误 -[__NSCFNumber countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xba794c0

标签: ios objective-c core-data


【解决方案1】:

假设您的数组a 不包含数字,而是字典,例如:

NSArray *a = @[
                  @{@"serverId" : @"2", ...},
                  @{@"serverId" : @"4", ...}
              ];

因为这会导致您收到的错误消息。

您必须创建一个仅包含“serverId”数字的数组,并在谓词中使用它:

NSArray *serverIds = [a valueForKey:@"serverId"];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"NOT (serverId IN %@)", serverIds];

【讨论】:

  • 你知道你是个天才吗。
  • 我在输入这段代码时很着急,因此我犯了一个愚蠢的错误,即给谓词提供了错误的变量
【解决方案2】:

我遇到了与 Developer 相同的问题,但在我的情况下,错误是我使用了错误的转换说明符。我的数组包含 NSNumber 对象,我使用 %ld 作为转换说明符。我确实按照 Martin R 的建议将转换说明符更改为 %@,它就像一个魅力。

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 2015-03-04
    • 2019-08-28
    • 2012-07-24
    相关资源
    最近更新 更多