Subclass MKAnnotation Problems

Last night I upgraded to Swift 1.2, and I got an error that I really can't understand. In previous versions of Xcode and Swift, this code worked fine.

//MARK: Annotation Object
class PointAnnotation : NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String
    var subtitle: String
    var point: Point
    var image: UIImage
    var md: String

    init(point: Point) {
        self.coordinate = point.coordinate
        self.title = point.title
        self.subtitle = point.teaser
        self.image = UIImage(named: "annotation.png")!
        self.point = point
        self.md = point.content
    }
}

In line 3, it becomes difficult for me to understand the error. Objective-C method 'setCoordinate:' provided by the setter for 'coordinate' conflicts with the optional requirement method 'setCoordinate' in protocol 'MKAnnotation'I tried to change the names of variables, etc., but did not help. Does anyone know how to fix this?

The class is intended for annotations on my map.

+4
source share
3 answers

If you do not need to change the coordinates after initialization, you can use it this way. This works for me with Swift 1.2:

class CustomAnnotation : NSObject, MKAnnotation {
    let coordinate: CLLocationCoordinate2D
    var title: String

    init(coordinate: CLLocationCoordinate2D, title: String) {
        self.coordinate = coordinate
        self.title = title
    }
}
+7
source

readarly ivar MKAnnotation readwrite.

var coordinate: CLLocationCoordinate2D { get }

0

, :

private var _coordinate: CLLocationCoordinate2D
var coordinate: CLLocationCoordinate2D {
    return _coordinate
}

init(coordinate: CLLocationCoordinate2D) {
    self._coordinate = coordinate
    super.init()
}

func setCoordinate(newCoordinate: CLLocationCoordinate2D) {
    self._coordinate = newCoordinate
}

That way, coordinateit is still read-only, and you can use the method setCoordinate.

0
source

All Articles