0

iOS初学者在这里。

给定一个具有多对多关系的实体

@class Categories;

@interface Events : NSManagedObject

@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSSet *categories;
@end

@interface Events (CoreDataGeneratedAccessors)

- (void)addCategoriesObject:(Categories *)value;
- (void)removeCategoriesObject:(Categories *)value;
- (void)addCategories:(NSSet *)values;
- (void)removeCategories:(NSSet *)values;

@end

与哪个有关

@interface Categories : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSManagedObject *events;

@end

并有一些 JSON 数据来填充事件表

[{"title": "Test Exhibition", "categories": [{"name": "Exhibitions"}]}, {...}]

我应该如何格式化 JSON 数据以执行类似的操作

// Parse Events JSON
[events enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
...
Events *event = [NSEntityDescription
    insertNewObjectForEntityForName:@"Events" inManagedObjectContext:context];
event.title = [obj objectForKey:@"title"];
NSSet *categories = [[[NSSet alloc] init] setByAddingObjectsFromArray:[obj objectForKey:@"categories"]];
NSLog(@"CATS %@", categories);
[event addCategories:categories];

最终让核心数据找到相关的类别(已经添加到数据库中)并将其附加到事件?

此刻我得到

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary entity]: unrecognized selector sent to instance 0x893a8c0'

任何帮助将不胜感激,谢谢!

4

1 回答 1

1

categories是一个NSSetNSDictionaries但是

- (void)addCategories:(NSSet *)values;

期望NSSetNSManagedObject's。因此,您必须NSManagedObject从每个描述类别的字典中创建一个,将它们添加到新的NSSet(或NSMutableSet)中,然后才addCategories:使用新的集合进行调用。

于 2013-10-10T10:04:41.563 回答