我以这种方式扩展了MKPointAnnotation课程:
class CustomPointAnnotation: MKPointAnnotation{
let eventID: Int
let coords: CLLocationCoordinate2D
var title: String? // error here
let location:String
init(eventID:Int, coords:CLLocationCoordinate2D, location:String, title:String?) {
self.eventID = eventID
self.coords = coords
self.title = title
self.location = location
super.init()
}
}
我收到一个错误:
Cannot override with a stored property 'title'
(我想如果我将成员重命名为 ,我会得到同样的错误coords)coordinate。
所以,我尝试了以下方法:
private var _title:String?
override var title: String? {
get { return _title }
set { _title = newValue }
}
但是,当我self.title = title在正文中添加时,init我得到:
'self' used in property access 'title' before 'super.init' call
如果我移到super.init()上面,我会得到两种错误:
Property 'self.eventID' not initialized at super.init call (1 error)Immutable value 'self.coords' may only be initialized once (repeated for every property)
title财产申报的正确方法是什么?有没有可能覆盖它?我发现了很多关于这个主题的问题,但没有扩展内置类的例子。任何帮助表示赞赏