Search code examples
swiftxcodeclassinitializationmkannotationview

Swift: Cannot assign to property: 'reuseIdentifier' is a get-only property


I am creating a custom MKAnnotationView class and trying to figure out the initializers for it. So far I have this:

class JumpSpotAnnotationView: MKAnnotationView {

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

override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
    super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
    self.annotation = annotation
    self.reuseIdentifier = reuseIdentifier
}

}

At that last line "self.reuseIdentifier = reuseIdentifier" I get the error 'Cannot assign to property: 'reuseIdentifier' is a get-only property'. I don't entirely understand what this means or how to fix it. How can I properly create the initializer for the reuseIdentifier? Also, as a side note, if you're feeling generous, could someone explain the whole "required init" thing? Im still new and would like to understand it, but the documentation is practically empty. Thanks!


Solution

  • You can remove the last two lines of init because you are calling super.init(annotation: annotation, reuseIdentifier: reuseIdentifier). So annotation and reuseIdentifier are already initialised in the init of your super class (which is MKAnnotationView) and therefore there is no need to do it in the subclass again.