I'm trying to add overlays to an MKMapView, avoiding duplicates by first looking for the overlay in the maps overlays (mapView.overlays: [MKoverlay]) store.
我正在尝试向MKMapView添加覆盖,通过首先查找映射覆盖(mapView)中的覆盖来避免重复。覆盖:[MKoverlay])。
This is what I'm trying currently
这就是我目前正在尝试的。
func addBoundary(points: [CLLocationCoordinate2D]) -> MKPolygon! {
var x = points
let polygon: MKPolygon = MKPolygon(coordinates: &x, count: points.count)
if overlays.indexOf(polygon as MKOverlay) == nil {
addOverlay(polygon)
return polygon
}
return nil
}
Yet, I'm getting the following error
但是,我得到了下面的错误
Cannot convert value of type 'MKOverlay' to expected argument type '@noescape (MKOverlay) throws -> Bool'
不能将类型'MKOverlay'的值转换为预期的参数类型'@noescape (MKOverlay)抛出-> Bool'
I'm guessing it's something simple. I've managed to do checks like this in the past with my own structs, but it doesn't seem to work on pre-defined arrays of common objects.
我猜这很简单。我曾经用我自己的结构做过这样的检查,但它似乎不能用于公共对象的预定义数组。
Can someone please shed some light on my mistakes. I'm guessing it's something to do with MKOverlay not being comparable - if that's the case, I've not certain how best to go about appending that functionality to a pre-defined object...
有人能解释一下我的错误吗?我猜这与MKOverlay没有可比性有关——如果是这样的话,我不确定如何将那个功能附加到一个预定义的对象……
Many thanks
非常感谢
p.s. This is Swift 2.0
注:这是Swift 2.0
I primarily tried simpler and more logical approach of using
我主要尝试了更简单、更合理的使用方法
if !overlays.contains(polygon) {
addOverlay(polygon)
}
...yielding an identical error - It seems likely that I'm missing something.
…产生一个相同的错误——似乎我漏掉了什么。
1 个解决方案
#1
1
As you surmised, it has to do with MKOverlay
not being comparable (specifically Equatable), and since MKOverlay
is a protocol, you can't extend it as you would a class. There's good explanation of this in this answer: How do I do indexOfObject or a proper containsObject
正如您所推测的,它与MKOverlay没有可比性(特别是Equatable)有关,而且由于MKOverlay是一个协议,所以不能像类那样扩展它。在这个答案中有很好的解释:我如何做indexOfObject或一个合适的containsObject
If you're sure that overlays
actually contains only MKPolygon
objects, try casting [MKOverlay]
to [MKPolygon]
:
如果您确信覆盖实际上只包含MKPolygon对象,请尝试将[MKOverlay]添加到[MKPolygon]:
let polygons = mapView.overlays as? [MKPolygon]
if polygons?.indexOf(polygon) == nil { ... }
MKPolygon
is an NSObject, so as of Swift 2, it automatically conforms to Equatable.
MKPolygon是一个NSObject,所以对于Swift 2,它自动符合Equatable。
Without trying it, it's unclear how MKPolygon
implements equality comparison. If it's the default NSObject pointer comparison, it won't work for your example, since you're instantiating new MKPolygon
objects. You could subclass MKPolygon
and implement -isEqual:
and -hash
based on your own logic to compare the coordinate points.
如果不尝试,就不清楚MKPolygon如何实现平等比较。如果它是默认的NSObject指针比较,那么它对您的示例不起作用,因为您正在实例化新的MKPolygon对象。您可以将MKPolygon子类化并实现-isEqual:和-hash基于您自己的逻辑来比较坐标点。
#1
1
As you surmised, it has to do with MKOverlay
not being comparable (specifically Equatable), and since MKOverlay
is a protocol, you can't extend it as you would a class. There's good explanation of this in this answer: How do I do indexOfObject or a proper containsObject
正如您所推测的,它与MKOverlay没有可比性(特别是Equatable)有关,而且由于MKOverlay是一个协议,所以不能像类那样扩展它。在这个答案中有很好的解释:我如何做indexOfObject或一个合适的containsObject
If you're sure that overlays
actually contains only MKPolygon
objects, try casting [MKOverlay]
to [MKPolygon]
:
如果您确信覆盖实际上只包含MKPolygon对象,请尝试将[MKOverlay]添加到[MKPolygon]:
let polygons = mapView.overlays as? [MKPolygon]
if polygons?.indexOf(polygon) == nil { ... }
MKPolygon
is an NSObject, so as of Swift 2, it automatically conforms to Equatable.
MKPolygon是一个NSObject,所以对于Swift 2,它自动符合Equatable。
Without trying it, it's unclear how MKPolygon
implements equality comparison. If it's the default NSObject pointer comparison, it won't work for your example, since you're instantiating new MKPolygon
objects. You could subclass MKPolygon
and implement -isEqual:
and -hash
based on your own logic to compare the coordinate points.
如果不尝试,就不清楚MKPolygon如何实现平等比较。如果它是默认的NSObject指针比较,那么它对您的示例不起作用,因为您正在实例化新的MKPolygon对象。您可以将MKPolygon子类化并实现-isEqual:和-hash基于您自己的逻辑来比较坐标点。