iphone - insertNewObjectForEntityForName : inManagedObjectContext: returning NSNumber bug?

标签 iphone core-data nsmanagedobject nsmanagedobjectcontext

我对 CoreData 比较熟悉,并且已经使用它好几年了,几乎没有任何困难。突然间,我被一个错误弄得目瞪口呆。对于我的一生,我无法弄清楚为什么

insertNewObjectForEntityForName:inManagedObjectContext:

突然返回某种奇怪的 NSNumber 实例。 GDB 表示返回的对象是 NSManagedObject 的正确自定义子类,但是当我去打印 NSManagedObject 本身的描述时,出现以下错误:

*** -[NSCFNumber objectID]: unrecognized selector sent to instance 0x3f26f50

更奇怪的是,我可以使用 setValue:forKey: 设置一些关系和属性,一切都很好。但是当我尝试设置一次特定关系时,出现此错误:

*** -[NSCFNumber entity]: unrecognized selector sent to instance 0x3f26f50

有人遇到过类似的事情吗?我尝试清理所有目标,重新启动所有内容,甚至将模型更改为有问题的关系是一对一而不是一对多。没有任何区别。

最佳答案

我之前遇到过“无法识别的选择器发送到实例 0x...”错误,当时我期望位于内存地址“指针”的对象已被其他内容替换。采取这种情况:

NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
NSString *someString = [NSString stringWithString:@"some string"]; // autoreleased object returned

[pool drain];
[pool release];

/*
some other code executes
*/

// since the string behind the someString variable has been autoreleased at this point, the memory that someString points to may be occupied by some other data type.  the following may through an EXC_BAD_ACCESS error, or it may try and execute the selector on whatever is occupying that memory space
int stringLength = [someString length];

这个例子非常简单,我的语义可能有点偏离,但是这有可能是你的例子中以更复杂的方式发生的事情吗?也许可以尝试:

[[NSEntityDescription insertNewObjectForEntityForName:@"entityName" inManagedObjectContext:managedObjectContext] retain]

看看会发生什么?

关于iphone - insertNewObjectForEntityForName : inManagedObjectContext: returning NSNumber bug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2739724/

相关文章:

ios - Azure 移动服务脱机数据同步 - pullWithQuery 错误

iphone - 为什么我无法使用此代码从委托(delegate)获取上下文?

iphone - IOS 上的图像处理过滤器,如白平衡、曝光、分离色调等

iphone - 使 iPhone 应用程序与 iOS 3 兼容

iphone - 调试器中没有符号,xCode 4

ios - NSManagedObject 的单元测试 [GHUnit]

ios - 在 NSManagedObject 类中初始化 @NSManaged 变量?

iphone - iOS 4.3.x上的sqlite比4.2.1慢得多

ios - 如何在谓词中引用对象 id?

objective-c - 核心数据获取请求不返回不同的结果