我正在研究tvOS應用程序,我希望能夠在註釋之間滑動。我將我的註釋存儲在字典中,並將字典存儲在數組中以備將來使用。我只是繪製了加拿大,美國和墨西哥的三個註釋。我希望能夠在這些註釋之間的遙控器上進行滑動。單擊註釋時,它會將您帶到有關該國家的信息。二元運算符'=='不能應用於類型'[MKAnnotation]'和'MKAnnotation'的操作數
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";
}]
當蘋果遙控器swipped,下,例如,我得到的,如果出現以下錯誤條件如下。
二元運算符「==」不能應用於類型的操作數「[MKAnnotation]」和「MKAnnotation」
我如何比較在arrary到一個註解選擇?
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)")
}
}
}
感謝您的回覆。您的建議將引發以下內容:'無法將'MKAnnotation'類型的值轉換爲期望的參數類型'@noescape(MKAnnotation)throws - > Bool'' –
@PaulS。這很奇怪。你使用的是什麼版本的Swift? – dasblinkenlight
這是因爲包含的非關閉版本要求元素是'MKAnnotation'不是'Equatable'。 – dan