我有一个一对多的coredata项目。这是我的.h:
@class Categories, Tags;
@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;
这是我的实现:
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];
NSMutableArray *tagArray = [ NSMutableArray new];
for (NSManagedObject *obj in tagsResults)
{
//firstName = [obj valueForKey:@"firstName"];
Tags *storeTag = [obj valueForKey:@"tag"];
[tagArray addObject:storeTag];
}
Content *content1 = [NSEntityDescription insertNewObjectForEntityForName:@"Content" inManagedObjectContext:moc];
content1.category = catego;
content1.title = _titleOutlet.stringValue;
content1.body = _bodyOutlet.stringValue;
NSMutableSet *tagSet = [NSMutableSet set];
for (int i = 0 ; i <tagArray.count; i++)
{
Tags *myTag = [tagArray objectAtIndex:i ];
[tagSet addObject:myTag];
}
content1.tags = tagSet;
但在这一行上,“content1.tags = tagSet;”我收到以下错误:
[NSTaggedPointerString managedObjectContext]: unrecognized selector sent to instance 0x696669637355
NSTaggedPointerString managedObjectContext]: unrecognized selector sent to instance 0x696669637355
如果您知道我在做错该怎么做的错误,我将不胜感激。
最佳答案
看来您的NSMutableSet *tagSet
包含不是NSString
实例的对象(可能是Tags
实例)。
在这些行上:
Tags *storeTag = [obj valueForKey:@"tag"];
[tagArray addObject:storeTag];
您要添加
tag
属性,而不是Tags
实例本身。也许只需将这两行替换为:
[tagArray addObject:obj];
关于ios - coredata一对多关系CoreDataGeneratedAccessors,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27573149/