Swift: 无法分配给 属性: 'reuseIdentifier' 是只获取 属性

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

我正在创建自定义 MKAnnotationView class 并试图找出它的初始值设定项。到目前为止我有这个:

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
}

}

在最后一行“self.reuseIdentifier = reuseIdentifier”我收到错误“无法分配给 属性:'reuseIdentifier' 是一个只获取 属性”。我不完全理解这意味着什么或如何解决它。如何正确创建 reuseIdentifier 的初始化程序?另外,作为旁注,如果您感到慷慨,有人可以解释整个“必需的初始化”吗?我还是新手,想了解它,但文档几乎是空的。谢谢!

您可以删除 init 的最后两行,因为您正在调用 super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)。所以 annotationreuseIdentifier 已经在你的超级 class 的 init 中初始化(即 MKAnnotationView),因此没有必要在subclass 再次.