【发布时间】:2015-09-08 10:17:05
【问题描述】:
我正在尝试使用 CoreData 和 Magical 记录。我的人际关系有问题 所以,我有部分,每个部分都应该存储产品 我的部分.h
@class Product;
@interface Section : NSManagedObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *code;
@property (nonatomic, retain) NSString *sectionId;
@property (nonatomic, retain) Product *product;
@end
@interface Section (CoreDataGeneratedAccessors)
- (void)addProductObject:(Product *)value;
- (void)removeProductObject:(Product *)value;
- (void)addProducts:(NSSet *)values;
- (void)removeProducts:(NSSet *)values;
和 Product.h
@interface Product : NSManagedObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *code;
@property (nonatomic, retain) NSString *descrpt;
@property (nonatomic, retain) NSString *imgBig;
@property (nonatomic, retain) NSString *imgSmall;
所以,我正在尝试将产品添加到这样的部分
for (int i = 0; i<[sectionResponse count]; i++) {
_section = [Section MR_createEntity];
_section.name = [[sectionResponse valueForKey:@"name"] objectAtIndex:i];
_section.code = [[sectionResponse valueForKey:@"code"] objectAtIndex:i];
_section.sectionId = [[sectionResponse valueForKey:@"id"] objectAtIndex:i];
for (int j = 0; j < productResponse.count; j++) {
if ([_section.sectionId isEqualToString:[[productResponse valueForKey:@"section"] objectAtIndex:j]]) {
_product = [Product MR_createEntity];
__product.name = [[productResponse valueForKey:@"name"] objectAtIndex:j];
_product.descrpt = [[productResponse valueForKey:@"desc"] objectAtIndex:j];
[_section addProductObject:_product];
}
}
}
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
能否请您帮助我了解如何添加产品以及以后如何检索它们?谢谢!
【问题讨论】:
-
作为一个局外人,我喜欢这个标题和介绍。神奇的记录、核心数据和关系问题:P
标签: ios core-data magicalrecord