【问题标题】:Swift: Cannot assign to property: 'reuseIdentifier' is a get-only propertySwift:无法分配给属性:“reuseIdentifier”是一个只能获取的属性
【发布时间】:2020-08-19 15:25:09
【问题描述】:

我正在创建一个自定义 MKAnnotationView 类并试图找出它的初始化程序。到目前为止,我有这个:

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' 是一个只能获取的属性”。我不完全明白这意味着什么或如何解决它。如何正确创建重用标识符的初始化程序?另外,作为旁注,如果您感觉很慷慨,有人可以解释整个“必需的初始化”吗?我还是新手,想了解它,但文档实际上是空的。谢谢!

【问题讨论】:

    标签: swift xcode class initialization mkannotationview


    【解决方案1】:

    您可以删除init 的最后两行,因为您正在调用super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)。所以annotationreuseIdentifier 已经在你的超类(即MKAnnotationView)的init 中初始化,因此无需在子类中再次进行。

    【讨论】:

      猜你喜欢
      • 2017-02-28
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      • 2022-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多