2015-03-27 15 views
5

nicht entsprechen Wenn ich versuche, mich anzupassen MKAnnotation Protokoll es Wurf Fehler meine Klasse entspricht nicht Protokoll MKAnnotation. Ich verwende den folgenden CodeKonnte MKAnnotation Protokoll in Swift

import MapKit 
import Foundation 

class MyAnnotation: NSObject, MKAnnotation 
{ 

} 

Das gleiche ist mit Objective-C möglich.

+0

prüfen diese Links aus, die gleiche Ausgabe http://stackoverflow.com/questions/24233873/mkannotation-swift –

Antwort

10

Sie müssen folgende erforderliche Eigenschaft in den Aufrufen implementieren:

class MyAnnotation: NSObject, MKAnnotation { 
    var myCoordinate: CLLocationCoordinate2D 

    init(myCoordinate: CLLocationCoordinate2D) { 
     self.myCoordinate = myCoordinate 
    } 

    var coordinate: CLLocationCoordinate2D { 
     return myCoordinate 
    } 
} 
+0

Wie Sie erwähnt haben, habe ich Coordinante-Eigenschaft implementiert, obwohl ich Fehler bekomme. Wenn Sie einen Democode haben, teilen Sie ihn bitte mit. Dank Klasse MyAnnotation: NSObject, MKAnnotation { var Koordinate: CLLocationCoordinate2D {get} } –

+0

Ich habe den Code geändert um genauer zu sein. – Abdullah

+0

Entfernen Sie die Deklaration 'var coordinate: ...' oberhalb der 'class ...' Definition. Das wird nur einen Compilerfehler verursachen und Leute verwirren. Es ist auch besser, zumindest in Beispielen, Titel und Untertitel zu implementieren, obwohl sie "optional" sind (Titel wird fast immer benötigt). – Anna

2

In Swift, können Sie sich alle nicht-optionale Variablen und Methoden eines Protokolls, um ein Protokoll zu entsprechen umsetzen müssen. Im Moment ist Ihre Klasse leer, was bedeutet, dass sie nicht konform ist mit dem Protokoll MKAnnotation. Wenn man sich die declaraton von MKAnnotation aussehen:

protocol MKAnnotation : NSObjectProtocol { 

    // Center latitude and longitude of the annotation view. 
    // The implementation of this property must be KVO compliant. 
    var coordinate: CLLocationCoordinate2D { get } 

    // Title and subtitle for use by selection UI. 
    optional var title: String! { get } 
    optional var subtitle: String! { get } 

    // Called as a result of dragging an annotation view. 
    @availability(OSX, introduced=10.9) 
    optional func setCoordinate(newCoordinate: CLLocationCoordinate2D) 
} 

können Sie sehen, dass, wenn Sie zumindest den coordinate Variable implementieren, dann entsprechen Sie das Protokoll.

1

Hier ist eine einfachere Version:

class CustomAnnotation: NSObject, MKAnnotation { 
    init(coordinate:CLLocationCoordinate2D) { 
     self.coordinate = coordinate 
     super.init() 
    } 
    var coordinate: CLLocationCoordinate2D 
} 

Sie benötigen keine zusätzliche Eigenschaft var myCoordinate: CLLocationCoordinate2D als akzeptierte Antwort zu definieren.

0

Alternativ (funktioniert in Swift 2.2, Xcode 7.3.1). (Anmerkung: Swift nicht auto-Benachrichtigungen bieten, so werfe ich in meinem eigenen) -

import MapKit 

class MyAnnotation: NSObject, MKAnnotation { 

// MARK: - Required KVO-compliant Property 
var coordinate: CLLocationCoordinate2D { 
    willSet(newCoordinate) { 
     let notification = NSNotification(name: "MyAnnotationWillSet", object: nil) 
     NSNotificationCenter.defaultCenter().postNotification(notification) 
    } 

    didSet { 
     let notification = NSNotification(name: "MyAnnotationDidSet", object: nil) 
     NSNotificationCenter.defaultCenter().postNotification(notification) 
    } 
} 


// MARK: - Required Initializer 
init(coordinate: CLLocationCoordinate2D) { 
    self.coordinate = coordinate 
} 
+0

danke für den Beispielcode. Ich vermute meine Frage hier: http://stackoverflow.com/questions/39273459/ios-mkmapview-custom-images-display-on-top-left-corner ist mit KVO verwandt. Aber wie kann mapView die Koordinatenänderung beobachten und die Benutzeroberfläche neu zeichnen? – cooltch