I had working code to remove all map annotations with a button, but after my update to xcode 7 I am running into the error:
我有工作代码用一个按钮删除所有地图注释,但在我更新到xcode 7后,我遇到了错误:
Type 'MKAnnotation' does not conform to protocol 'SequenceType'
类型'MKAnnotation'不符合协议'SequenceType'
if let annotations = (self.mapView.annotations as? MKAnnotation){
for _annotation in annotations {
if let annotation = _annotation as? MKAnnotation {
self.mapView.removeAnnotation(annotation)
}
}
}
3 个解决方案
#1
47
In Swift 2 annotations
is declared as non optional array [MKAnnotation]
so you can easily write
在Swift 2中,注释被声明为非可选数组[MKAnnotation],因此您可以轻松编写
let allAnnotations = self.mapView.annotations
self.mapView.removeAnnotations(allAnnotations)
without any type casting.
没有任何类型的铸造。
#2
14
self.mapView.removeAnnotations(self.mapView.annotations)
If you don't want remove user location.
如果您不想删除用户位置。
self.mapView.annotations.forEach {
if !($0 is MKUserLocation) {
self.mapView.removeAnnotation($0)
}
}
Note: Objective-C now have generics, it is no longer necessary cast the elements of 'annotations' array.
注意:Objective-C现在有了泛型,不再需要强制转换'annotations'数组的元素。
#3
0
The issue is that there are two methods. One is removeAnnotation which takes an MKAnnotation object, and the other is removeAnnotations which takes an array of MKAnnotations, note the "s" at the end of one and not the other. Attempting to cast from [MKAnnotation]
, an array to MKAnnotation
a single object or visa versa will crash the program. The line of code self.mapView.annotations creates an array. Thus, if you are using the method removeAnnotation, you need to index the array for a single object within the array, as seen below:
问题是有两种方法。一个是removeAnnotation,它接受一个MKAnnotation对象,另一个是removeAnnotations,它接受一个MKAnnotations数组,注意一个结尾的“s”而不是另一个。试图从[MKAnnotation],一个数组转换为MKAnnotation单个对象或反之亦然会使该程序崩溃。代码行self.mapView.annotations创建一个数组。因此,如果您使用方法removeAnnotation,则需要为数组中的单个对象索引数组,如下所示:
let previousAnnotations = self.mapView.annotations
if !previousAnnotations.isEmpty{
self.mapView.removeAnnotation(previousAnnotations[0])
}
Thus, you can remove various annotations while keeping the users location. You should always test your array before attempting to remove objects from it, else there is the possibly getting an out of bounds or nil error.
因此,您可以在保留用户位置的同时删除各种注释。您应该在尝试从中删除对象之前测试您的数组,否则可能会出现一个越界或零错误。
Note: using the method removeAnnotations (with an s)removes all annotations. If you are getting a nil, that implies that you have an empty array. You can verify that by adding an else statement after the if, like so;
注意:使用removeAnnotations(带有s)方法会删除所有注释。如果你得到一个零,这意味着你有一个空数组。您可以通过在if之后添加else语句来验证,就像这样;
else{print("empty array")}
#1
47
In Swift 2 annotations
is declared as non optional array [MKAnnotation]
so you can easily write
在Swift 2中,注释被声明为非可选数组[MKAnnotation],因此您可以轻松编写
let allAnnotations = self.mapView.annotations
self.mapView.removeAnnotations(allAnnotations)
without any type casting.
没有任何类型的铸造。
#2
14
self.mapView.removeAnnotations(self.mapView.annotations)
If you don't want remove user location.
如果您不想删除用户位置。
self.mapView.annotations.forEach {
if !($0 is MKUserLocation) {
self.mapView.removeAnnotation($0)
}
}
Note: Objective-C now have generics, it is no longer necessary cast the elements of 'annotations' array.
注意:Objective-C现在有了泛型,不再需要强制转换'annotations'数组的元素。
#3
0
The issue is that there are two methods. One is removeAnnotation which takes an MKAnnotation object, and the other is removeAnnotations which takes an array of MKAnnotations, note the "s" at the end of one and not the other. Attempting to cast from [MKAnnotation]
, an array to MKAnnotation
a single object or visa versa will crash the program. The line of code self.mapView.annotations creates an array. Thus, if you are using the method removeAnnotation, you need to index the array for a single object within the array, as seen below:
问题是有两种方法。一个是removeAnnotation,它接受一个MKAnnotation对象,另一个是removeAnnotations,它接受一个MKAnnotations数组,注意一个结尾的“s”而不是另一个。试图从[MKAnnotation],一个数组转换为MKAnnotation单个对象或反之亦然会使该程序崩溃。代码行self.mapView.annotations创建一个数组。因此,如果您使用方法removeAnnotation,则需要为数组中的单个对象索引数组,如下所示:
let previousAnnotations = self.mapView.annotations
if !previousAnnotations.isEmpty{
self.mapView.removeAnnotation(previousAnnotations[0])
}
Thus, you can remove various annotations while keeping the users location. You should always test your array before attempting to remove objects from it, else there is the possibly getting an out of bounds or nil error.
因此,您可以在保留用户位置的同时删除各种注释。您应该在尝试从中删除对象之前测试您的数组,否则可能会出现一个越界或零错误。
Note: using the method removeAnnotations (with an s)removes all annotations. If you are getting a nil, that implies that you have an empty array. You can verify that by adding an else statement after the if, like so;
注意:使用removeAnnotations(带有s)方法会删除所有注释。如果你得到一个零,这意味着你有一个空数组。您可以通过在if之后添加else语句来验证,就像这样;
else{print("empty array")}