gpt4 book ai didi

ios - NSPredicate 用于检测属性的类

转载 作者:行者123 更新时间:2023-11-29 03:35:01 26 4
gpt4 key购买 nike

我有事件,每个事件与 NSManagedObject Entertainment 有一对一关系,而 Entertainment 有一对多关系,并且有 NSSet 事件

@interface Event : NSManagedObject
@property (nonatomic, retain) NSDate *creationDate;
@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSString * objectId;
@property (nonatomic, retain) NSString * price;
@property (nonatomic, retain) Entertainment *entertainment;
@property (nonatomic, retain) Place *place;
@property (nonatomic, retain) NSString *townName;
- (void)fillProperties:(NSDictionary *)JSON;
@end

@interface Entertainment : NSManagedObject
@property (nonatomic, retain) NSString * additionalInfo;
@property (nonatomic, retain) NSNumber * allCommentsLoaded;
@property (nonatomic, retain) id image;
@property (nonatomic, retain) NSString * imageURL;
@property (nonatomic, retain) NSString * info;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * objectId;
@property (nonatomic, retain) NSSet *comments;
@property (nonatomic, retain) NSSet *events;
- (void)fillProperties:(NSDictionary *)JSON;
@end

娱乐有很多子类所以我需要创建这样的 NSFetchedResultController 来获取具有特定类娱乐的事件

我试过

- (NSFetchedResultsController *)fetchedResultsControllerForEventsFromEntertainment:(Class)className 
{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Event class])];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"entertainment isMemberOfClass: %@", [className class]];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"objectId" ascending:YES];
fetchRequest.sortDescriptors = @[sortDescriptor];
fetchRequest.fetchBatchSize = 20;
NSFetchedResultsController *fetchResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
return fetchResultsController;
}

但是我弄错了:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Unknown/unsupported comparison predicate operator type'
***

我读到,当我们使用 SQLite 时,这个方法 isMemberOfClass 不起作用,因为 SQLite 不知道这样的方法。在通常的 NSPredicate 中,例如简单的数组,它必须工作。

那么我如何从 SQLite 中获取具有特定类娱乐的事件呢?

最佳答案

不可能在谓词中引用实体名称或类名称一个(基于 SQLite 的)核心数据获取请求。

但是只获取获取请求中指定的实体的对象,而不是对象任何子实体,只需设置

[fetchRequest setIncludesSubentities:NO];

关于ios - NSPredicate 用于检测属性的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19289185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com