简体   繁体   English

无法将类型“ [AnyObject]”的值转换为预期的参数类型“ [MKAnnotation]”错误

[英]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.

相关问题 无法将“NSMutableDictionary”类型的值转换为预期的参数类型“[String: AnyObject]?” - Cannot convert value of type 'NSMutableDictionary' to expected argument type '[String: AnyObject]?' 无法转换NSMutableDictionary类型的值?预期的参数类型[NSObject:AnyObject]! - Cannot convert value of type NSMutableDictionary? to expected argument type [NSObject: AnyObject]! 无法将“String”类型的值转换为预期的参数类型“AnyObject?” - Cannot convert value of type 'String' to expected argument type 'AnyObject?' 无法将类型“ [AnyObject]”的值转换为预期的参数类型“ [UIViewController]?” - Cannot convert value of type'[AnyObject]'to expected argument type '[UIViewController]?' Swift类别:“无法将类型的值转换为预期的参数类型&#39;AnyObject!&#39; - Swift category: "Cannot convert value of type to expected argument type 'AnyObject!' 无法转换类型为“任何对象!”的值 到预期的参数类型“ Int” - cannot convert value of type 'Anyobject!' to expected argument type 'Int' 无法将&#39;NSObject - &gt;() - &gt; PostFeed&#39;类型的值转换为预期的参数类型&#39;AnyObject?&#39; - Cannot convert value of type 'NSObject -> () -> PostFeed' to expected argument type 'AnyObject?' Swift 2无法将'[AnyObject]'类型的值转换为预期的参数类型'[UIViewController]?' - Swift 2 Cannot convert value of type '[AnyObject]' to expected argument type '[UIViewController]?' 无法转换类型[[String:AnyObject] ?. Type”(也称为“可选” <Dictionary<String, AnyObject> &gt; .Type)到期望的参数类型 - Cannot convert value of type '[String : AnyObject]?.Type' (aka 'Optional<Dictionary<String, AnyObject>>.Type) to expected argument type 无法转换“inout NSNumber”类型的值? 到预期的参数类型 &#39;AutoreleasingUnsafeMutablePointer<AnyObject?> &#39; 错误 - Cannot convert value of type 'inout NSNumber?' to expected argument type 'AutoreleasingUnsafeMutablePointer<AnyObject?>' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM