1 - 是的。有可能的。创建映射文件时,请确保将关系的类型从 NSSet 更改为 NSArray,如下所示:
@class Tags
@interface Entity : NSManagedObject
...
@property (nonatomic, retain) NSOrderedSet *tags;
...
@end
@interface Entity (CoreDataGeneratedAccessors)
...
- (void)addEntityTags:(NSOrderedSet *)values;
- (void)removeEntityTags:(NSOrderedSet *)values;
...
@end
改成:
@class Tags
@interface Entity : NSManagedObject
...
@property (nonatomic, retain) NSArray *tags;
...
@end
@interface Entity (CoreDataGeneratedAccessors)
...
- (void)addEntityTags:(NSArray *)values;
- (void)removeEntityTags:(NSArray *)values;
...
@end
2 - 您可以在创建获取请求时指定排序描述符,如下所示:
NSFetchRequest *fetchRequest [NSFetchRequest new];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:myManagedObjectContext];
[fetchRequest.setEntity: entity];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"tags" ascending:<YES|NO>]];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"ANY tags == %@", yyy]];
NSFetchResultsController *myFetchResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:myManagedObjectContext setcionNameKeyPath:nil cacheName:nil];
myFetchResultsController.delegate = self;
[fetchRequest release]; // Forget that if using ARC.
NSError *error = nil;
if (![myFetchResultsController performFetch:&error]) {
NSLog(@"Failed to fetch data: %@", error);
}
3 - 如果您使用该属性,则不需要为您的标签指定谓词。如果您想进一步对标签进行排序,您可以简单地在该数组上使用排序描述符,如下所示:
NSSortDescriptor *tagDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"<tags field to sort on>" ascending:<YES|NO>];
NSArray *sortedTags = [tagsProperty sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptors]];
您可以简单地从该属性中读取标签。
希望这可以帮助!