您可以使用结构而不是字典。
快速例子,让我们做一个总线对象:
struct Bus {
var name: String
var direction: String
var times: [NSDate]
}
它保存每个总线的值:总线,其方向,和日期总线时间的阵列的名称。
当然这只是一个例子,你必须适应你的用例。
您创建总线:
let bus1 = Bus(name: "Busey MacBusface", direction: "Somewhere", times: [...some dates...])
let bus2 = Bus(name: "Gary Bussey", direction: "Nowhere", times: [...some dates...])
你可以让他们在收藏:
let todayBusses = [bus1, bus2]
容易过滤:
let allDirections = todayBusses.map { $0.direction }
let allTimes = todayBusses.map { $0.times }
let someBusses = todayBusses.filter { $0.times.contains({ (date) in
// compare 'date' with other date(s)
}) }
let notNowhere = todayBusses.filter { $0.direction != "Nowhere" }
等。再次,这
只是一个快速的前例充足,但我认为它可以给你想法。
“的时代公共汽车的具体名称”会是这样的:
let times = todayBusses.filter { $0.name == "theBusName" }.map { $0.times }
你将如何得到时间的公共汽车的具体名称?我需要搜索总线,然后打印出阵列中的项目? – Recusiwe
我已经用示例更新了我的答案。请注意,我正在使用地图和过滤器来处理我的示例,但如果您对此不熟悉,则还可以照常使用循环。 – Moritz
目前,我有一个停止类和一个带有derpartures的类,它包含在一个数组的stop类中。所以你只需要创建另一个类/结构并循环遍历所有的出发点,以确保它们被添加到正确的总线对象中?事情就是,同一辆公交车将在JSON中重复多次,因为它每天有几班出发。所以我想我必须循环查看我的公交列表,并检查我所在的那个是否已经在那里,如果不是,我会创建一个对象并添加它,否则我只需添加时间当前对象的列表? – Recusiwe