Binary operator '==' cannot be applied to operands of type '[MKAnnotation]' and 'MKAnnotation'

别说谁变了你拦得住时间么 提交于 2019-12-25 15:53:45

问题


I'm working on a tvOS app, where I want to be able to swipe between annotations. I store my annotations in a Dictionary, and store the Dictionary in an Array for future use. I am simply plotting three annotations for Canada, USA, and Mexico. I want to be able to swipe on the remote between these annotations. When an annotation is clicked, it will take you to information about that country.

let countriesArray = ["Canada", "Mexico", "United States of America"]  
var annotationArray = [MKPointAnnotation]()  
var locationDictionary = [String: AnyObject]()  
var locationArray = [AnyObject]()  

Once the three countries are plotted, my array looks like the following:

locationArray [{  
    annotation = "<MKPointAnnotation: 0x7fc021b01f70>";  
    country = Canada;  
    latitude = "71.47385399229037";  
    longitude = "-96.81064609999999";  
}, {  
    annotation = "<MKPointAnnotation: 0x7fc0219dcf90>";  
    country = "United States of America";  
    latitude = "37.99472997055178";  
    longitude = "-95.85629150000001";  
}, {  
    annotation = "<MKPointAnnotation: 0x7fc02260ff70>";  
    country = Mexico;  
    latitude = "23.94480686844645";  
    longitude = "-102.55803745";  
}]  

When the Apple remote is swipped, down, for example, I'm getting the following error on the if condition below.

Binary operator '==' cannot be applied to operands of type '[MKAnnotation]' and 'MKAnnotation'

How can I compare the annotation in the arrary to the one selected?

func swipedDown(sender:UISwipeGestureRecognizer) {  
    print("Down")  

    let selectedAnnotation = mapView.selectedAnnotations  

    for x in 0 ... locationArray.count - 1 {  

        let value = locationArray[x].valueForKey("annotation") as! MKAnnotation  

        if  selectedAnnotation == value {   //error

            let currentLocation = locationArray[x].valueForKey("country")  

            print("Your on \(currentLocation)")  
        }  
    }  
}  

回答1:


Since mapView.selectedAnnotations returns an array of MKAnnotations, i.e. an [MKAnnotation], comparing it to a single MKAnnotation value with operator == does not work. You need to decide what to do with multiple values from the array - specifically, whether the comparison should be true when any of the annotations in the array matches value, or when the first one matches value, or all of them match value.

If you need to check that one array element matches the value, you can use contains, like this:

if  (selectedAnnotation.contains { $0.title == value.title && $0.subtitle == value.subtitle }) {
    let currentLocation = locationArray[x].valueForKey("country")  
    print("Your on \(currentLocation)")  
}

Edit: The initial attempt at using non-closure contains has failed, because MKAnnotation does not conform to Equatable protocol. Thanks, dan, for catching this!




回答2:


1- [MKAnnotation] is an array of MKAnnotation , can you notice the difference?

2- let selectedAnnotation = mapView.selectedAnnotations here, you are storing all the annotations not the current one, can you notice the s in this statement mapView.selectedAnnotations ?



来源:https://stackoverflow.com/questions/38376706/binary-operator-cannot-be-applied-to-operands-of-type-mkannotation-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!