获取数组
问题描述:
阵列内与最大的对象数组我有CLLocations数组的数组,我试图寻找一种有发生最近根据其时间戳CLLocation对象数组。索引或实际列表都足够了。获取数组
到目前为止,我想出了:
let latestList = listOfListOfLocations.index{$0.max{a,b in a.timestamp < b.timestamp} < $1.max{c,d in c.timestamp < d.timestamp}}
,但我发现没有运气,不能让我的头轮它是相对较新的斯威夫特。任何帮助将不胜感激。
答
首先得到每个数组的“submaxes”,然后拿这些的最大值,以获得全局最大值。
let subArrayContainingMostRecentObject = listOfListOfLocations
.flatMap{ subarray in
return subarray
.max{ $0.timestamp < $1.timestamp }
.map{ submax in (subarray: subarray, submax: submax) }
}
.max{ $0.submax.timestamp < $1.submax.timestamp }?.subarray
答
不知道这是最充分的实现,但它的工作原理。
let mostRecent = listOfListOfLocations.max{$0.max{$0.timestamp < $1.timestamp}! < $1.max{$0.timestamp < $1.timestamp}!}?.max()
+0
你最好做一个通过每个子数组来获得它们的最大值,然后通过submaxes数组来获得全局最大值。否则这种方式,随时随地两个数组进行比较,每个子阵列的submax必须重新计算 – Alexander
@MartinR谢谢,稍后我会更新 – Alexander
那太好了,谢谢,我怎么也更改为获得指数? – user3599895
@ user3599895哪个索引? – Alexander