超出 KVC 生成数组的范围

发布于 2024-08-05 08:57:26 字数 615 浏览 4 评论 0原文

我试图从其他对象的层次结构中创建一个对象数组,如下所示:

代码 1:

childController.names = [[NSMutableArray alloc] init];

for (Person *p in list.persons) {
    [childController.names addObject:p.name];
}

代码 2:

NSMutableArray *testArray = [list.persons valueForKey:@"name"];

第一个代码片段运行良好,返回一个人名数组。相反,第二个返回一个包含正确人数姓名的数组,但当我在 XCode 中调试代码时,它们显示为“超出范围”。当我尝试访问其中之一时,应用程序终止并显示:

由于未捕获而终止应用程序 例外 'NSInvalidArgumentException',原因: '*** -[NSCFSet objectAtIndex:]: 无法识别的选择器发送到实例 0x72e0620'

列表,人员是核心数据管理对象(如果重要的话)。我做错了什么???

I'm trying to create an array of objects from a hierarchy of other objects like this:

code 1:

childController.names = [[NSMutableArray alloc] init];

for (Person *p in list.persons) {
    [childController.names addObject:p.name];
}

code 2:

NSMutableArray *testArray = [list.persons valueForKey:@"name"];

The first code snippet works perfectly, returning an array of persons' names. In contrast, the second returns an array with the correct number of persons' names, but they are shown as "Out of scope" when I debug the code in XCode. When I'm trying to access one of them, the app terminates with:

Terminating app due to uncaught
exception
'NSInvalidArgumentException', reason:
'*** -[NSCFSet objectAtIndex:]:
unrecognized selector sent to instance
0x72e0620'

The list, persons are Core Data managed objects if it matters.. What am I doing wrong???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福还没到 2024-08-12 08:57:26

您应该检查人员的类型。看起来它是一个 NSSet,而 NSSet 上的 valueForKey: 将返回一个 NSSet。此代码片段应该按预期工作:

NSSet* testSet = [list.persons valueForKey:@"name"];

You should check the type of persons. It seems like it is a NSSet, and valueForKey: on NSSet will return a NSSet. This code snippet should work as expected:

NSSet* testSet = [list.persons valueForKey:@"name"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文