从一个到多个关系中从NSSet获取数据时,Core Data崩溃

时间:2015-08-11 09:15:50

标签: ios objective-c xcode core-data

我是核心数据关系的新手,现在我的一对多关系代码面临崩溃。代码段位于

之下
    -(void)fetchDataFromArray {
   NSArray *tempArray = [[CLCoreDataAdditions sharedInstance]  getAllDocuments:@"MainCategory"];
    for (int i =0; i<tempArray.count; i++) {
        MainCategory *mainCategory = [tempArray objectAtIndex:i];
        NSLog(@"%@",mainCategory.categoryId);
    }


    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.categoryId == %@", [NSNumber numberWithInt:1]];
    NSArray *tempArray1 = [[CLCoreDataAdditions sharedInstance] getAllDocumentsFor:@"MainCategory" withPredicate:predicate];
    NSLog(@"Temp %@",tempArray1);
    for (int i =0; i<tempArray1.count; i++) {
        MainCategory *mainCategory = [tempArray1 objectAtIndex:i];
        NSLog(@"%@",mainCategory.categoryId);
        //NSLog(@"%@",mainCategory.subCategoryforMain);
        if(mainCategory.subCategoryforMain.count != 0) {
            NSArray *array = [mainCategory.subCategoryforMain allObjects];
            for (int j=0; j<array.count; j++) {
                SubCategory *subCategory = [array objectAtIndex:j];
                NSLog(@"%@",subCategory.categoryName);

            }
        }
    }
}

崩溃图片如下:

enter image description here

这是界面声明

@property (nonatomic, retain) NSNumber * categoryId;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *subCategoryforMain;
@end

@interface MainCategory (CoreDataGeneratedAccessors)

- (void)addSubCategoryforMainObject:(SubCategory *)value;
- (void)removeSubCategoryforMainObject:(SubCategory *)value;
- (void)addSubCategoryforMain:(NSSet *)values;
- (void)removeSubCategoryforMain:(NSSet *)values;

0 个答案:

没有答案