二进制运算符'=='不能应用于类型'[MKAnnotation]'和'MKAnnotation'的操作数 [英] Binary operator '==' cannot be applied to operands of type '[MKAnnotation]' and 'MKAnnotation'
问题描述
我正在使用tvOS应用程序,希望在其中注释之间滑动.我将注释存储在字典中,并将字典存储在数组中以备将来使用.我只是在为加拿大,美国和墨西哥绘制三个注释.我希望能够在这些注释之间的遥控器上滑动.单击注释后,它将带您到有关该国家的信息.
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";
}]
例如,当Apple遥控器向下滑动时,在以下if条件下出现以下错误.
When the Apple remote is swipped, down, for example, I'm getting the following error on the if condition below.
二进制运算符'=='不能应用于类型'[MKAnnotation]'和'MKAnnotation'的操作数
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)")
}
}
}
推荐答案
由于mapView.selectedAnnotations
返回一个MKAnnotation
s数组,即一个[MKAnnotation]
,将其与单个MKAnnotation
值与operator ==
进行比较,所以不行.您需要决定如何处理数组中的多个值-具体来说,当数组中的任何注释与value
匹配时,或者当第一个与value
匹配时,或者所有它们都匹配时,比较是否应为true value
.
Since mapView.selectedAnnotations
returns an array of MKAnnotation
s, 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
.
如果需要检查一个数组元素是否与值匹配,则可以使用contains
,如下所示:
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)")
}
:由于MKAnnotation
不符合Equatable
协议,首次尝试使用非封闭式contains
失败.谢谢 dan 赶上了!
The initial attempt at using non-closure contains
has failed, because MKAnnotation
does not conform to Equatable
protocol. Thanks, dan, for catching this!
这篇关于二进制运算符'=='不能应用于类型'[MKAnnotation]'和'MKAnnotation'的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!