Ios 如何将自定义特性从Mapbox注释传递到其详图索引视图?

Ios 如何将自定义特性从Mapbox注释传递到其详图索引视图?,ios,swift,mapbox,Ios,Swift,Mapbox,我将MGLAnnotation子类化,以添加额外的属性var标记,如下所示: class MasterMapAnnotation: NSObject, MGLAnnotation { var coordinate: CLLocationCoordinate2D var title: String? var subtitle: String? var tags: String? init(coordinate: CLLocationCoordinate2

我将
MGLAnnotation
子类化,以添加额外的属性
var标记
,如下所示:

class MasterMapAnnotation: NSObject, MGLAnnotation {

    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?
    var tags: String?

    init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String?, tags: String?) {

        self.coordinate = coordinate
        self.title = title
        self.subtitle = subtitle
        self.tags = tags

    }

}

以下是传递到地图中的注释:

let annotation = MasterMapAnnotation(coordinate: CLLocationCoordinate2D(latitude: 50.0, longitude: 50.0), title: "Numero Uno", subtitle: "Numero Uno subtitle", tags: "#tag1 #tag2")
mapView.addAnnotation(annotation)

mapView代理调用自定义详图索引视图:

func mapView(_ mapView: MGLMapView, calloutViewFor annotation: MGLAnnotation) -> MGLCalloutView? {

    return MasterMapCalloutView(representedObject: annotation)

}

因此,在callout视图子类(下面)中,我如何访问这个新属性
标记

class MasterMapCalloutView: UIView, MGLCalloutView {

    var representedObject: MGLAnnotation
    let dismissesAutomatically: Bool = false
    let isAnchoredToAnnotation: Bool = true
    lazy var leftAccessoryView = UIView()
    lazy var rightAccessoryView = UIView()
    weak var delegate: MGLCalloutViewDelegate?

    required init(representedObject: MGLAnnotation) {
        self.representedObject = representedObject
        super.init(frame: .zero)
    }

    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // callout view delegate: present callout
    func presentCallout(from rect: CGRect, in view: UIView, constrainedTo constrainedView: UIView, animated: Bool) {

        if !representedObject.responds(to: #selector(getter: MGLAnnotation.title)) {
            return
        }

        view.addSubview(self)

        let title = representedObject.title
        let tags = representedObject.tags // how to access this?

    }

}


问题很明显,子类(
mastermapanotation
)拥有自定义的
标记
属性,而不是它的超类(
MGLAnnotation
),但我不知道如何(a)将representedObject设置为
mastermapanotation
)或(b)只需访问
MasterMapAnnotation
自定义属性,而无需重新配置协议。

我可能遗漏了一些内容,但您只需检查是否可以将MGLAnnotation对象强制转换为MasterMapAnnotation:

guard let masterAnnotation : MasterMapAnnotation = representedObject as? MasterMapAnnotation else { return }

let tags : String? = masterAnnotation.tags

// etc

显然,除了使用guard之外,还有其他控制流量的方法,但我希望你能理解要点。

不,你没有遗漏任何东西,我只是个新手。我不知道在哪里演出,我所有的尝试都失败了。@sconewolf不用担心,我们都需要从某个地方开始!再次感谢兄弟,你帮了我很多。快速提问帮助我更好地理解转换:你将
representedObject
向下转换到自定义子类(这对我来说非常有意义);您是否也可以将
masterAnnotation
改为
MGLAnnotation