addObject to MutableSet崩溃 - 原因:'to-many关系的值不可接受

时间:2012-02-24 02:56:35

标签: iphone objective-c core-data

我试图将“to many”关系对象添加到现有条目中。 我的代码看起来像这样:

    User *user = [self fetchUserwithInformation:firstName lastName:lastName];
    NSMutableSet *userRecord;
    userRecord = [person mutableSetValueForKey: @"userInformation"];

    Information *information = (Information *)[NSEntityDescription insertNewObjectForEntityForName:@"Information" inManagedObjectContext:managedObjectContext];

    information.age = @"99";
    information.gender = @"m";

    NSSet *set = [NSSet setWithObject:information];
    [userRecord addObject:information];

此代码因未捕获的异常而崩溃:

'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-many relationship: property = "userInformation"; desired type = NSSet; given type = __NSArrayM; value = ( ...some values...

我想我正在添加一个NSSet(setWithObject:information)......?

提前致谢

1 个答案:

答案 0 :(得分:0)

您正在将数组information添加到userRecord,而不是您使用该数组创建的集。

错误消息正在告诉您问题的确切原因 - 您正在将数组传递给期望设置的内容。

我很惊讶Xcode没有警告你set变量未被使用。