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];
最终让Core Data找到相关类别(已添加到数据库中)并将其附加到事件中?
目前我
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary entity]: unrecognized selector sent to instance 0x893a8c0'
非常感谢任何帮助,谢谢!
答案 0 :(得分:1)
categories
是NSSet
的{{1}},但
NSDictionaries
预计- (void)addCategories:(NSSet *)values;
NSSet
。因此,您必须从描述类别的每个字典中创建NSManagedObject's
,将其添加到新的NSManagedObject
(或NSSet
),然后再使用新的NSMutableSet
调用addCategories:
集。