2016-07-14 1 views
0

주석간에 스 와이프 할 수있는 tvOS 앱에서 작업하고 있습니다. 사전에 주석을 저장하고 나중에 사용할 수 있도록 사전에 배열을 저장합니다. 저는 캐나다, 미국 및 멕시코에 대한 세 가지 주석을 단순히 그려 둡니다. 이 주석 사이에서 리모컨을 스 와이프하고 싶습니다. 주석을 클릭하면 해당 국가에 대한 정보로 이동합니다. 세 나라가 그려되면이진 연산자 '=='은 '[MKAnnotation]'및 'MKAnnotation'유형의 피연산자에 적용 할 수 없습니다.

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

, 내 배열처럼 보이는 다음 : 애플 리모트가 swipped 때

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"; 
}] 

가, 아래로, 예를 들어, 내가 만약에 다음과 같은 오류를 받고 있어요 아래 조건.

이진 연산자 "== '형식의 피연산자'[MKAnnotation] '를 적용 할 수 없습니다'MKAnnotation '

I가 하나로 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)") 
     } 
    } 
} 

답변

1

mapView.selectedAnnotations 이후 리턴 MKAnnotation의 배열, 즉 [MKAnnotation]operator ==MKAnnotation있는 단일 값과 비교하여 동작하지 않는다. 배열의 여러 값으로 처리 할 작업을 결정해야합니다. 구체적으로 배열의 주석이 value과 일치 할 때 또는 첫 번째 것이 value과 일치하거나 모두가 value과 일치 할 때 비교가 true인지를 결정해야합니다. 당신은 하나의 배열 요소의 값이 일치하는지 확인해야하는 경우

,이 같은 contains를 사용할 수 있습니다

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

편집 : 비 폐쇄 contains를 사용에서 최초의 시도가 실패, MKAnnotation을하기 때문에 는 Equatable 프로토콜을 따르지 않습니다. 감사합니다, 이것을 잡기 위해 dan!

+0

답변 해 주셔서 감사합니다. 귀하의 제안은 다음을 던졌습니다 : 'MKAnnotation'유형의 값을 예상 인수 유형 '@noescape (MKAnnotation) throws -> Bool'' –

+0

@PaulS로 변환 할 수 없습니다. 이것은 매우 이상합니다. 스위프트의 어떤 버전을 사용하고 있습니까? – dasblinkenlight

+0

contains의 non-closure 버전은'MKAnnotation'이 아닌'Equatable' 요소를 필요로하기 때문입니다. – dan

1

1- [MKAnnotation]MKAnnotation의 배열입니다. 차이점을 확인할 수 있습니까?

2 개 - let selectedAnnotation = mapView.selectedAnnotations 여기에 현재 주석이 아닌 모든 주석을 저장하고 있습니다.이 문장에서 s를 확인할 수 있습니까? mapView.selectedAnnotations?

+1

.first를'let selectedAnnotation = mapView.selectedAnnotations.first'에 추가하면 첫 번째 주석이 선택됩니다. 비교를 'let value = locationArray [x]로 변경합니다.valueForKey ("annotation") as! MKPointAnnotation 선택한 경우 별명을! MKPointAnnotation == value { '작동하는 것 같습니다. –

관련 문제