2017-02-15 3 views
-1

У меня есть карта с замечательными комментариями. Я хочу иметь сегментированный контроль для изменения аннотаций.MKAnotation - не может назначить значение типа x для ввода MKAnnotation

этот код

annotations = getMapAnnotations() 
// Add mappoints to Map 
mapInfView.addAnnotations(annotations) 

работает в ViewDidLoad, но я пытаюсь реализовать его после изменения файла Plist, чтобы получить данные. Я не понимаю, почему я получаю Не могу присвоить значение типа ['Stands'] для tupe MKAnnotation? когда он находится в сегментированном коде управления, а не в viewdidload. Есть ли лучший способ изменить аннотации?

getannotations()

func getMapAnnotations() -> [Stands] { 
    var annotations:Array = [Stands]() 
    print("ANNOTATIONS") 
    //load plist file 
    var stands: NSArray? 
    print("(FILE \(iFile)") 
    if let path = Bundle.main.path(forResource: iFile, ofType: "plist")  { 
     stands = NSArray(contentsOfFile: path) 
     print(path) 
     print(stands) 
    } 

//iterate and create annotations 
if let items = stands { 
    for item in items { 
    let lat = (item as AnyObject).value(forKey: "lat") as! Double 
    let long = (item as AnyObject).value(forKey: "long")as! Double 
    let annotation = Stands(latitude: lat, longitude: long) 
    let tit = (item as AnyObject).value(forKey: "title") as! String 
    let sub = (item as AnyObject).value(forKey: "subtitle") as! String 

    annotation.title = tit //"\(numb) \(tit)" 
    annotation.subtitle = sub 

    annotations.append(annotation) 
    } 
} 
return annotations 

}

трибунами Класс

import Foundation 

import MapKit 

class Stands: NSObject, MKAnnotation { 
    var title: String? 
    var subtitle: String? 
    var no: Int? 
    var latitude: Double 
    var longitude:Double 

    var coordinate: CLLocationCoordinate2D { 
    return CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 
    } 

    init(latitude: Double, longitude: Double) { 
    self.latitude = latitude 
    self.longitude = longitude 
    } 
} 

Кодекса IBAction

@IBAction func annotType(_ sender: Any) { 
    let infoChoice = AnnotType(rawValue: infoSegmentController.selectedSegmentIndex) 
    switch (infoChoice!) { 
    case .WC: 
     iFile = "wc" 
    case .Restaurant: 
     iFile = "restaurant" 
    case .ATM: 
     iFile = "atm" 
    case .Museums: 
     print("Museums") 
     iFile = "museums" 

} 
print("END") 
annotations = getMapAnnotations() 
// Add mappoints to Map 
mapInfView.addAnnotations(annotations) 

}

+1

Какие _is_ 'аннотации'? Вы назначаете его, но где/как оно объявляется? Вы должны иметь «var annotations» где-то; покажи это. - Также покажите весь ваш 'viewDidLoad', где вы утверждаете, что это работает. Держу пари, что это не то же самое. – matt

ответ

0

матовый был прав. В нагрузке ViewDid я имел

var annotations = getMapAnnotations() 

в @IBAction Func annotType()

У меня был

annotations = getMapAnnotations() 

Добавление вар объявить его снова решить эту проблему.

сразу после объявления класса, наряду со многими другими объявлениями переменных я имел

var annotations:MKAnnotations? 

Это, кажется, не служат никакой цели. Я просто прокомментировал это, и приложение работает нормально.