coredata访问数据关系错误

时间:2014-12-19 22:48:48

标签: ios objective-c xcode core-data

我的coredata项目是我的.h文件:

@interface Content : NSManagedObject

@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * body;
@property (nonatomic, retain) Categories *category;
@property (nonatomic, retain) NSSet *tags;
@end

@interface Content (CoreDataGeneratedAccessors)

- (void)addTagsObject:(Tags *)value;
- (void)removeTagsObject:(Tags *)value;
- (void)addTags:(NSSet *)values;
- (void)removeTags:(NSSet *)values;

我正在按照以下方式填充我的coredata:

    NSError *error = nil;

    // Categories:

    NSManagedObjectContext *moc = [self managedObjectContext];
    NSEntityDescription *contentDescription = [ NSEntityDescription entityForName:@"Categories" inManagedObjectContext:moc];

    NSFetchRequest *categoRequest = [NSFetchRequest new];
    categoRequest.entity = contentDescription;
    NSPredicate *categoPredicate = [NSPredicate predicateWithFormat:@"category like %@", _dropMenuOulet.stringValue];
    categoRequest.predicate = categoPredicate;

    NSArray *results = [moc executeFetchRequest:categoRequest error:&error];

    Categories *catego = (Categories*) [results objectAtIndex:0];

//    Tags:

    NSEntityDescription *tagsDescription = [ NSEntityDescription entityForName:@"Tags" inManagedObjectContext:moc];
    NSFetchRequest *tagsRequest = [NSFetchRequest new];
    tagsRequest.entity = tagsDescription;
    NSArray *tagsResults = [moc executeFetchRequest:tagsRequest error:&error];


    Content *content1 = [NSEntityDescription insertNewObjectForEntityForName:@"Content" inManagedObjectContext:moc];

    content1.category = catego;
    content1.title = _titleOutlet.stringValue;
    content1.body = _bodyOutlet.stringValue;

    NSMutableSet  *tagSet = [NSMutableSet set];


    for (NSManagedObject *obj in tagsResults)
    {
        [tagSet addObject:obj];
    }

    content1.tags = tagSet;

我现在的问题是,当我尝试访问内容中的标记时,我得到以下内容:

tags = "<relationship fault: 0x60000002d820 'tags'>";

如何访问内容的标签?

1 个答案:

答案 0 :(得分:0)

我想通过不同的方式做到这一点:

NSError *error = nil;
    NSManagedObjectContext *moc = [self managedObjectContext];
    NSEntityDescription *contentDescription = [ NSEntityDescription entityForName:@"Content" inManagedObjectContext:moc];
    NSFetchRequest *contentRequest = [NSFetchRequest new];
    contentRequest.entity = contentDescription;
    NSArray *results = [moc executeFetchRequest:contentRequest error:&error];
    NSLog(@"results content %@",results);

    Content *myCont = [results objectAtIndex:0];

    NSLog(@"list of tags %@", [[myCont tags] valueForKey:@"tag"]);