Swift 二进制运算符'==';无法应用于类型为';[MKAnnotation]';和';MKAnnotation';
我正在开发一个tvOS应用程序,我希望能够在注释之间滑动。我将注释存储在字典中,并将字典存储在数组中以备将来使用。我只是在为加拿大、美国和墨西哥绘制三个注释。我希望能够在这些注释之间在遥控器上滑动。单击注释时,您将看到有关该国家的信息Swift 二进制运算符'==';无法应用于类型为';[MKAnnotation]';和';MKAnnotation';,swift,tvos,mkannotationview,Swift,Tvos,Mkannotationview,我正在开发一个tvOS应用程序,我希望能够在注释之间滑动。我将注释存储在字典中,并将字典存储在数组中以备将来使用。我只是在为加拿大、美国和墨西哥绘制三个注释。我希望能够在这些注释之间在遥控器上滑动。单击注释时,您将看到有关该国家的信息 let countriesArray = ["Canada", "Mexico", "United States of America"] var annotationArray = [MKPointAnnotation]() var locationDi
let countriesArray = ["Canada", "Mexico", "United States of America"]
var annotationArray = [MKPointAnnotation]()
var locationDictionary = [String: AnyObject]()
var locationArray = [AnyObject]()
绘制这三个国家后,我的数组如下所示:
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";
}]
由于
mapView.selectedAnnotations
返回一个MKAnnotation
数组,即[MKAnnotation]
,将其与带有运算符==
的单个MKAnnotation
值进行比较不起作用。您需要决定如何处理数组中的多个值—具体来说,当数组中的任何注释与值
匹配时,或者当第一个注释与值
匹配时,或者当所有注释都与值
匹配时,比较是否应为真
如果需要检查一个数组元素是否与值匹配,可以使用contains
,如下所示:
if (selectedAnnotation.contains { $0.title == value.title && $0.subtitle == value.subtitle }) {
let currentLocation = locationArray[x].valueForKey("country")
print("Your on \(currentLocation)")
}
Edit:由于
MKAnnotation
不符合equalable
协议,首次尝试使用非闭包contains
失败。谢谢,谢谢你抓住这个 自mapView.selectedAnnotations
返回一个MKAnnotation
数组,即[MKAnnotation]
,将其与带有运算符==
的单个MKAnnotation
值进行比较不起作用。您需要决定如何处理数组中的多个值—具体来说,当数组中的任何注释与值
匹配时,或者当第一个注释与值
匹配时,或者当所有注释都与值
匹配时,比较是否应为真
如果需要检查一个数组元素是否与值匹配,可以使用contains
,如下所示:
if (selectedAnnotation.contains { $0.title == value.title && $0.subtitle == value.subtitle }) {
let currentLocation = locationArray[x].valueForKey("country")
print("Your on \(currentLocation)")
}
Edit:由于
MKAnnotation
不符合equalable
协议,首次尝试使用非闭包contains
失败。谢谢,谢谢你抓住这个 1-[MKAnnotation]
是一个由MKAnnotation
组成的数组,您能注意到其中的区别吗
2-
让selectedAnnotation=mapView。selectedAnnotations
在这里,您存储的是所有注释,而不是当前注释,您能注意到此语句中的s吗mapView。selectedAnnotations
?1-[MKAnnotation]
是一个MKAnnotation
数组,您能注意到区别吗
2-
让selectedAnnotations=mapView。selectedAnnotations
在这里,您存储的是所有注释,而不是当前注释,您能注意到此语句中的s吗mapView。selectedAnnotations
?感谢您的回复。您的建议引发以下问题:无法将“MKAnnotation”类型的值转换为预期的参数类型“@noescape(MKAnnotation)throws->Bool”
@PaulS。这很奇怪。您使用的是哪个版本的Swift?这是因为contains的非封闭版本要求元素是equalable
,而MKAnnotation
则不是。@dan很好,非常感谢!我进行了编辑以解决这个问题。Xcode版本7.3.1(7D1014)。当我进行编辑时,我得到以下二进制运算符'=='不能应用于'String??'和'String?'类型的操作数。
感谢您的回复。您的建议引发以下问题:无法将“MKAnnotation”类型的值转换为预期的参数类型“@noescape(MKAnnotation)throws->Bool”
@PaulS。这很奇怪。您使用的是哪个版本的Swift?这是因为contains的非封闭版本要求元素是equalable
,而MKAnnotation
则不是。@dan很好,非常感谢!我进行了编辑以解决这个问题。Xcode版本7.3.1(7D1014)。当我进行编辑时,我得到以下二进制运算符'=='不能应用于'String??'和'String?'类型的操作数。
添加。首先添加到让selectedAnnotation=mapView。selectedAnnotations。首先
选择第一个注释。将我的比较更改为let value=locationArray[x].valueForKey(“注释”)as!MKPointAnnotation如果选择了Annotation as!MKPointAnnotation==value{
似乎有效。将.first添加到let selectedAnnotation=mapView.selectedAnnotations.first
选择第一个批注。将比较更改为let value=locationArray[x]。valueForKey(“批注”)as!MKPointAnnotation如果选择,则说明as!MKPointAnnotation==value{
似乎有效。