在我的Lion应用中,我有这样的数据模型:

Item内的关系子项是有序的。

Xcode 4.1 (build 4B110)为我创建了文件Item.h, Item.h。m, SubItem.h和SubItem.h。

下面是Item.h的内容(自动生成):

#import <Foundation/Foundation.h>

#import <CoreData/CoreData.h>

@class SubItem;

@interface Item : NSManagedObject {
@private
}

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSOrderedSet *subitems;
@end

@interface Item (CoreDataGeneratedAccessors)

- (void)insertObject:(SubItem *)value inSubitemsAtIndex:(NSUInteger)idx;
- (void)removeObjectFromSubitemsAtIndex:(NSUInteger)idx;
- (void)insertSubitems:(NSArray *)value atIndexes:(NSIndexSet *)indexes;
- (void)removeSubitemsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectInSubitemsAtIndex:(NSUInteger)idx withObject:(SubItem *)value;
- (void)replaceSubitemsAtIndexes:(NSIndexSet *)indexes withSubitems:(NSArray *)values;
- (void)addSubitemsObject:(SubItem *)value;
- (void)removeSubitemsObject:(SubItem *)value;
- (void)addSubitems:(NSOrderedSet *)values;
- (void)removeSubitems:(NSOrderedSet *)values;

@end

下面是Item.m的内容(自动生成):

#import "Item.h"
#import "SubItem.h"

@implementation Item

@dynamic name;
@dynamic subitems;

@end

如您所见,Item类提供了一个名为addSubitemsObject:的方法。不幸的是,当你试图以这种方式使用它时:

Item *item = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:self.managedObjectContext];
item.name = @"FirstItem";

SubItem *subItem = [NSEntityDescription insertNewObjectForEntityForName:@"SubItem" inManagedObjectContext:self.managedObjectContext];

[item addSubitemsObject:subItem];

出现以下错误:

2011-09-12 10:28:45.236 Test[2002:707] *** -[NSSet intersectsSet:]: set argument is not an NSSet

你能帮我吗?

更新:

在我的错误报告刚刚过去1787天之后,今天(2016年8月1日),苹果公司给我写了这样的话:“请用最新的iOS 10测试版验证这个问题,并在bugreport.apple.com上更新你的错误报告。”让我们希望这是正确的时间:)


当前回答

相反,我建议使用NSObject中的访问器来访问关系的NSMutableOrderedSet。

- (void)addSubitemsObject:(SubItem *)value {
      NSMutableOrderedSet* tempSet = [self mutableOrderedSetValueForKey:@"subitems"];
      [tempSet addObject:value];
 }

例如,iOS v5.0的核心数据发布说明参考了这一点。

在一个简短的测试中,它在我的应用程序中工作。

其他回答

就我个人而言,我刚刚用@Stephan在另一个解决方案中概述的方法直接调用替换了对CoreData生成方法的调用:

NSMutableOrderedSet* tempSet = [self mutableOrderedSetValueForKey:@"subitems"];
      [tempSet addObject:value];
[tempSet addObject:value];

这消除了对类别的需求,当错误修复后,这些类别可能与苹果的解决方案和生成的代码发生冲突。

这有一个额外的好处,就是它是官方的方式!

SWIFT正确答案的更好版本

var tempSet = NSMutableOrderedSet()
if parent!.subItems != nil {
    tempSet = NSMutableOrderedSet(orderedSet: parent!.subItems!)
}

tempSet.add(newItem)
parent!.subItems = tempSet

我通过谷歌错误信息找到了这个问题,只是想指出我以一种稍微不同的方式(不是使用有序集)遇到了这个错误。这并不完全是对给定问题的答案,但我把它贴在这里,以防它对其他在搜索时偶然发现这个问题的人有帮助。

我正在添加一个新的模型版本,并向现有模型添加了一些关系,并在头文件中自己定义了add*Object方法。当我尝试调用它们时,我得到了上面的错误。

在检查了我的模型之后,我意识到我愚蠢地忘记了勾选“to - many Relationship”复选框。

所以如果你遇到这种情况,你没有使用有序集,仔细检查你的模型。

我同意这里可能有漏洞。我已经修改了添加对象>setter的实现,以正确地追加到NSMutableOrderedSet。 - (void)addSubitemsObject:(SubItem *)value { NSMutableOrderedSet* tempSet = [NSMutableOrderedSet orderedSetWithOrderedSet:self.subitems]; [tempSet addObject:价值); 自我。subitems = tempSet; } 将集合重新分配给self。子项将确保发送will /DidChangeValue通知>。

leell,你确定在这样的自定义设置NSMutableOrderedSet值存储在该集将被CoreData正确地保存到数据库吗?我没有检查,但它看起来像CoreData对NSOrderedSet一无所知,并期望NSSet作为多关系容器。

罗伯特,

我同意你的答案可以解决这个问题,但是请记住,已经有一种自动创建的方法可以将一整套值添加到关系中。苹果的文档(见“多对关系”部分或“自定义多对关系访问器方法”部分)是这样实现的:

- (void)addEmployees:(NSSet *)value
{
[self willChangeValueForKey:@"employees"
      withSetMutation:NSKeyValueUnionSetMutation
      usingObjects:value];
[[self primitiveEmployees] unionSet:value];
[self didChangeValueForKey:@"employees"
      withSetMutation:NSKeyValueUnionSetMutation
      usingObjects:value];
}

- (void)removeEmployees:(NSSet *)value
{
[self willChangeValueForKey:@"employees"
      withSetMutation:NSKeyValueMinusSetMutation
      usingObjects:value];
[[self primitiveEmployees] minusSet:value];
[self didChangeValueForKey:@"employees"
      withSetMutation:NSKeyValueMinusSetMutation
      usingObjects:value];
}

您可以很容易地在核心数据之外编译您的关系集,然后使用此方法一次性添加它们。它可能没有你建议的方法那么难看;)