[英]Cannot convert value of type '[AnyObject]' to expected argument type '[MKAnnotation]' error
I've just updated to Xcode 7 and translated my code to Swift 2.0, however I have run into an issue with a particular part of my code that I can't quite figure out. 我刚刚更新到Xcode 7,并将我的代码转换为Swift 2.0,但是我遇到了我的代码中某个特定部分的问题,我无法弄清楚。
This is the code: 这是代码:
let annotationsToRemove = (self.mapView.annotations as NSArray).mutableCopy() as! NSMutableArray
annotationsToRemove.removeObjectsInArray(objects as [AnyObject])
self.mapView.removeAnnotations(annotationsToRemove as [AnyObject])
let annotationsToAdd = objects.mutableCopy() as! NSMutableArray
annotationsToAdd.removeObjectsInArray(self.mapView.annotations)
On the middle line, I'm getting the error: 在中间一行,我得到了错误:
Cannot convert value of type '[AnyObject]' to expected argument type '[MKAnnotation]'
无法将类型“ [AnyObject]”的值转换为预期的参数类型“ [MKAnnotation]”
Can anyone suggest what I should be changing this line to? 谁能建议我将这一行更改为什么?
Thanks. 谢谢。
Supposing that objects
is [MKAnnotation]
, you could write 假设
objects
是[MKAnnotation]
,则可以编写
let annotationsToRemove = self.mapView.annotations.filter { (annotation) -> Bool in !objects.contains { $0 === annotation} }
self.mapView.removeAnnotations(annotationsToRemove)
let annotationsToAdd = objects.filter { (object) -> Bool in !self.mapView.annotations.contains { $0 === object} }
In Xcode 7 / Swift 2 annotations
is declared as [MKAnnotation]
, a non-optional Swift Array
. 在Xcode 7 / Swift 2中,
annotations
被声明为[MKAnnotation]
,这是一个非可选的Swift Array
。 It makes things so much easier. 它使事情变得容易得多。
Now just write 现在写
let annotationsToRemove = self.mapView.annotations
self.mapView.removeAnnotations(annotationsToRemove)
No type casting, no MSMutableArray
's 没有类型转换,没有
MSMutableArray
的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.