【发布时间】:2017-12-04 15:19:01
【问题描述】:
我创建了两个函数来计算从我的位置到 x 车的距离
func distance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance {
let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
let to = CLLocation(latitude: to.latitude, longitude: to.longitude)
return from.distance(from: to)
}
func selT(car:Car) {
guard let coordinates = car.location else {
return
}
self.destination = coordinates
if currentLocation != nil {
let dist = distance(from: currentLocation!, to: coordinates)
}
}
我有一个下载汽车的功能
func getOkToDownload(download:CarsToDownload?, error : Error?) -> Void {
if let download = download {
self.download = download
if download.status == "OK" {
if let carsDownloaded = download.cars {
var number = numberCars / (categories?.count)!
number = number == 0 ? 1 : number
let distanceC = carsDownloaded
cars.append(contentsOf: carsDownloaded)
}
self.tableView?.reloadData()
} else {
let alert = UIAlertController.init(title: "Error", message: response.status, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction.init(title: "Retry", style: .default, handler: { (action) in
DispatchQueue.main.async {
self.showCars(true)
}
}))
self.present(viewController: alert)
}
showing = false
} else {
print("response is nil")
}
}
其中“汽车”是var cars: [Car] = [],“下载”是CarsToDownload 类型,所以download.cars 是我要下载的汽车,现在我要做的是创建一个按顺序排列的过滤器这个函数 (getOkToDownload) 我从最近下载到最公平的汽车,为此我想我必须创建一个循环来计算 carsDownloaded 的所有距离,一旦我获得了值,用 a 对数组排序过滤,但我该怎么做?考虑到我用我展示给你的那些外部函数得到了一辆 x 车的距离值,现在我只在我的Car 类中添加了这个参数var distanceT: Float?(不知道它是否有用)
【问题讨论】: