我的代码有问题。 我收到此错误消息:
无法将类型'CLLocationCoordinate2D'的值转换为预期的参数类型'(CLLocationCoordinate2D)throws-> Bool'
if(locationManager.allowsBackgroundLocationUpdates && isDriving){
locationManager.startUpdatingLocation()
guard let locValue: CLLocationCoordinate2D = locationManager.location?.coordinate else { return }
Map.setCenter(locValue, animated: true)
Map.setRegion(MKCoordinateRegion(center: locValue, latitudinalMeters: 75, longitudinalMeters: 75), animated: true)
if !locations.contains(where: locValue) { //<- ERROR
locations.append(locValue);
NSLog("%f %f -> Gesamt: %d", locValue.latitude, locValue.longitude, locations.count);
let polyline = MKPolyline(coordinates: &locations, count: locations.count);
Map.addOverlay(polyline);
}
}
位置:
var locations = [CLLocationCoordinate2D]()
答案 0 :(得分:3)
CLLocationCoordinate2D
不符合Equatable
。
您必须在latitude
闭包中同时比较longitude
和where
if !locations.contains(where: {$0.latitude == locValue.latitude && $0.longitude == locValue.longitude}) { ...
答案 1 :(得分:2)
使用
extension CLLocationCoordinate2D : Equatable {
static public func ==(left: CLLocationCoordinate2D, right: CLLocationCoordinate2D) -> Bool {
return left.latitude == right.latitude && left.longitude == right.longitude
}
}
if !locations.contains(locValue){
}
要使数组使用contains
,元素需要符合Equatable
,并且由于CLLocationCoordinate2D
不符合,因此错误修复需要您添加where子句以指定比较方式会像