gpt4 book ai didi

ios - 一对多关系查询中的核心数据

转载 作者:行者123 更新时间:2023-11-29 02:16:09 25 4
gpt4 key购买 nike

我有一个这样的核心数据对象:

enter image description here

我构建了一个谓词来获取日程安排时间和我需要获取电影和剧院的每个选择时间

我试图利用时间表的值(value)来获得剧院

这是我的代码:

 NSString *nameOfMovie = [[scheduleResults movie] valueForKey:@"nameOfMovie"];

NSPredicate *theaterPredicate = [NSPredicate predicateWithFormat:@"SUBQUERY (showTimes, $x, $x.nameOfMovie == %@ AND $x.showTimes.showTimes == %@).@count > 0", nameOfMovie, showTimes];

但是我的谓词出现了这个错误:

此处不允许对多 key

你们中有人知道我的谓词做错了什么吗?

最佳答案

我发现这个问题很有趣,所以用 swift 创建了一个简单的 Playground 来测试你的案例的子查询。我认为你在数据模型中缺少一些关系,如果你真的关心它,你应该以单数命名实体,如电影、时间表、剧院等,这更有意义,因为实体总是单数,关联可以是单数或复数取决于一对多或一对一关系。

这是我提出的数据模型,

// a Movie can have multiple schedule and a schedule can have multiple movies at the same time
Schedule << ------- >> Movie

// a Movie can be running in multiple theater and a theater can have multiple movies
Movie << ------>> Theater

因此,基于上述关系,我创建了一个 Playground 文件来创建一些夹具数据并进行游戏,

class Schedule: NSObject {
var showTime: NSDate!
var movies: [Movie]!
}

class Theater: NSObject {
var name: String!
var movies: [Movie]!
}

class Movie: NSObject {
var nameOfMovie: String!
var theaters: [Theater]!
var showTimes: [Schedule]!
}

let hitech = Theater()
hitech.name = "Hitech Cinema"

let kino = Theater()
kino.name = "Kino"

let warner = Theater()
warner.name = "Warners"


let interstellar = Movie()
interstellar.nameOfMovie = "Interstellar"

let gravity = Movie()
gravity.nameOfMovie = "Gravity"

let wrathOfSpace = Movie()
wrathOfSpace.nameOfMovie = "Wrath of Space"

let today = NSDate()
let yesterday = today.dateByAddingTimeInterval(-60 * 60 * 24)
let tomorrow = today.dateByAddingTimeInterval(60 * 60 * 24)


let todaySchedule = Schedule()
todaySchedule.showTime = today

let yesterdaySchedule = Schedule()
yesterdaySchedule.showTime = yesterday

let tomorrowSchedule = Schedule()
tomorrowSchedule.showTime = tomorrow


todaySchedule.movies = [interstellar, gravity]
tomorrowSchedule.movies = [interstellar, wrathOfSpace]
yesterdaySchedule.movies = [wrathOfSpace, gravity]

interstellar.showTimes = [todaySchedule, tomorrowSchedule]
gravity.showTimes = [todaySchedule, yesterdaySchedule]
wrathOfSpace.showTimes = [tomorrowSchedule, yesterdaySchedule]

interstellar.theaters = [kino, hitech]
gravity.theaters = [kino, warner]
wrathOfSpace.theaters = [warner, hitech]

kino.movies = [interstellar, gravity]
hitech.movies = [interstellar, wrathOfSpace]
warner.movies = [wrathOfSpace, gravity]


let theaters:NSArray = [hitech, warner, kino]


var nameOfMovie = "Gravity"
var date = yesterday

var predicate = NSPredicate(format: "SUBQUERY(movies, $a, $a.nameOfMovie = %@ and SUBQUERY($a.showTimes, $b, $b.showTime = %@).@count > 0).@count > 0", nameOfMovie, date)

let threatersYesterdayGravity = theaters.filteredArrayUsingPredicate(predicate)


nameOfMovie = "Interstellar"
date = tomorrow

predicate = NSPredicate(format: "SUBQUERY(movies, $a, $a.nameOfMovie = %@ and SUBQUERY($a.showTimes, $b, $b.showTime = %@).@count > 0).@count > 0", nameOfMovie, date)

let threatersTomorrowInterstellar = theaters.filteredArrayUsingPredicate(predicate)

结果如我所料。您还可以使用上述关系和子查询来满足您的需要。

关于ios - 一对多关系查询中的核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28728656/

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