gpt4 book ai didi

ios - 在实体 中找不到 coredata keypath nameOfMovie

转载 作者:行者123 更新时间:2023-11-28 18:59:02 26 4
gpt4 key购买 nike

我有一个 coredata 项目,我正在尝试进行查询。这是我的核心数据模型:

enter image description here

NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *theaterDescription = [ NSEntityDescription entityForName:@"Theaters" inManagedObjectContext:moc];

NSPredicate *theaterPredicate = [NSPredicate predicateWithFormat:@"nameOfTheater like %@ AND nameOfMovie like %@",_theaterName,_movieOutlet.stringValue];


NSFetchRequest *theaterRequestTwo = [NSFetchRequest new];
theaterRequestTwo.entity = theaterDescription;
theaterRequestTwo.predicate = theaterPredicate;



NSError *error = nil;

NSArray *theaterResults = [moc executeFetchRequest:theaterRequestTwo error:&error];

但是我收到了这个错误:

keypath nameOfMovie not found in entity <NSSQLEntity Theaters id=3>

我也试过:

NSPredicate *theaterPredicate = [NSPredicate predicateWithFormat:@"nameOfTheater like %@ AND Movies.nameOfMovie like %@",_theaterName,_movieOutlet.stringValue];

但是我得到了这个错误:

keypath Movies.nameOfMovie not found in entity <NSSQLEntity Theaters id=3>

我也试过:

NSPredicate *theaterPredicate = [NSPredicate predicateWithFormat:@"nameOfTheater like %@ AND movies.nameOfMovie like %@",_theaterName,_movieOutlet.stringValue];

我收到以下错误:

此处不允许对多 key

你们中的任何人都可能知道我做错了什么或我的代码中缺少什么。非常感谢您的帮助。

更新:

这是我的核心数据模型的头文件:

剧院类:

@class Movies;

@interface Theaters : NSManagedObject

@property (nonatomic, retain) NSString * nameOfTheater;
@property (nonatomic, retain) NSSet *movies;
@end

@interface Theaters (CoreDataGeneratedAccessors)

- (void)addMoviesObject:(Movies *)value;
- (void)removeMoviesObject:(Movies *)value;
- (void)addMovies:(NSSet *)values;
- (void)removeMovies:(NSSet *)values;

电影类:

@class Schedules, Theaters;

@interface Movies : NSManagedObject

@property (nonatomic, retain) NSString * nameOfMovie;
@property (nonatomic, retain) NSSet *showTimes;
@property (nonatomic, retain) Theaters *theaters;
@end

@interface Movies (CoreDataGeneratedAccessors)

- (void)addShowTimesObject:(Schedules *)value;
- (void)removeShowTimesObject:(Schedules *)value;
- (void)addShowTimes:(NSSet *)values;
- (void)removeShowTimes:(NSSet *)values;

调度类:

@interface Schedules : NSManagedObject

@property (nonatomic, retain) NSDate * showTimes;
@property (nonatomic, retain) NSSet *movie;
@end

@interface Schedules (CoreDataGeneratedAccessors)

- (void)addMovieObject:(Movies *)value;
- (void)removeMovieObject:(Movies *)value;
- (void)addMovie:(NSSet *)values;
- (void)removeMovie:(NSSet *)values;

最佳答案

你可以使用SUBQUERY对于这类问题

一个确切的剧院实体(由您的示例复制)

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nameOfTheater like %@ AND SUBQUERY(movies, $mv, $mv.nameOfMovie like %@).@count > 0", _theaterName,_movieOutlet.stringValue];

更新

正在放映电影abc的所有影院

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(movies, $mv, $mv.nameOfMovie CONTAINS[c] %@).@count > 0", @"abc"];

顺便说一句,你的模型正确吗?

Theaters <->> Movies = 1:n

所以每个 Theater有 x Movies ,而是一个 Movie仅在一个 Theater 中运行因此,如果您获取 Movie使用名称“abc”你可以获得Theater作为属性。或者是吗

Theaters <<->> Movies = n:m

?所以moviesmovieTheatersNSSet<Movie>/NSSet<Theater>

更新 2

上面的问题还需要一个答案。之间的正确关系是什么?是 x 家影院或仅一家影院的一部电影。 ManagedObject 类的头文件如何? ;)

NSEntityDescription *schedulesDescription = [NSEntityDescription entityForName:@"Schedules" inManagedObjectContext:moc];

// something like this if 1:n. Try and post logs, have no IDE at the moment
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"movie.nameOfMovie == %@ AND movie.movieTheaters.nameOfTheater == %@", @"movie8", @"theaterOne"];

// if n:m, as far as I remember you couldn't fetch over 2 n:m relations via .
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"movie.nameOfMovie == %@ AND SUBQUERY(movie, $mv, $mv.movieTheaters.nameOfTheater == %@).@count > 0", @"movie8", @"theaterOne"];

关于ios - 在实体 <NSSQLEntity Theaters id=3> 中找不到 coredata keypath nameOfMovie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28436816/

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