【发布时间】:2009-11-09 12:50:08
【问题描述】:
我有一个使用 Core Data 的应用程序,该应用程序具有我想要分组的关系。我有由我的 .xcdatamodel 文件生成的 NSManagedObject 类,而且大部分情况下一切似乎都正常。
鉴于父/子关系,我想做以下事情:
一个父母有一个孩子的集合。孩子们有一个属性 groupByProperty,我想对它进行分组。
以下代码:
NSSet *allChildren = parent.children;
NSArray *groups = [allChildren valueForKeyPath:@"@distinctUnionOfObjects.groupByProperty"];
Child *child = [groups objectAtIndex:x]; //x is the row that I would like to retrieve
在尝试设置子指针时产生 NSInvalidArgumentException。
但是,当我这样做时:
NSSet *allChildren = parent.children;
NSArray *groups = [[NSArray alloc] initWithArray:[allChildren valueForKeyPath:@"@distinctUnionOfObjects.groupByProperty"]];
Child *child = [groups objectAtIndex:x]; //x is the row that I would like to retrieve
一切正常。
谁能解释这种行为?我要疯了,想弄清楚它是如何工作的。
提前感谢您提供的任何帮助...
克里斯
【问题讨论】:
-
您显示的代码没有什么不同,除了您泄漏了在第二个示例中分配的数组(因为您没有向它发送发布消息,至少我们可以看到)。请编辑您的问题以包含打印到调试器控制台的完整异常消息。
-
这是错误。似乎从 valueForKeyPath: 返回的对象实际上不是一个 NSArray。不过这很奇怪,因为 initWithArray: 需要一个 NSArray* 传递给它……这很奇怪! *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*** -[NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x3a3ff60”
-
另外,Peter,我正在代码中进一步发布组。为了简单起见,我制作了这个 sn-p 以显示导致崩溃的原因。
标签: iphone objective-c cocoa cocoa-touch core-data