MKAnnotation - 无法分配给 属性:'title' 是一个 get-only 属性
MKAnnotation - Cannot assign to property: 'title' is a get-only property
如何解决这个构建问题 - 我只是想为 mapkit 创建一个注释:
错误:"MKAnnotation - Cannot assign to property: 'title' is a get-only property"
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Annotations
var annotation:MKAnnotation
annotation.title = "test" // ** ISSUE IS HERE **
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
备注:
- 已下载最新版本 XCode
- 这与 Swift 2.0 有关吗?
-
MKAnnotation 要求对象继承自 NSObjectProtocol。为此,您应该让 class 继承自 NSObject。您应该声明您的属性以匹配 MKAnnotation 协议的属性。
示例:
class MapPin : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
}
}
用法:
override func viewDidLoad() {
super.viewDidLoad()
// Annotations
var annotation:MapPin
annotation.title = "test"
}
如何解决这个构建问题 - 我只是想为 mapkit 创建一个注释:
错误:"MKAnnotation - Cannot assign to property: 'title' is a get-only property"
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Annotations
var annotation:MKAnnotation
annotation.title = "test" // ** ISSUE IS HERE **
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
备注: - 已下载最新版本 XCode - 这与 Swift 2.0 有关吗? -
MKAnnotation 要求对象继承自 NSObjectProtocol。为此,您应该让 class 继承自 NSObject。您应该声明您的属性以匹配 MKAnnotation 协议的属性。
示例:
class MapPin : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
}
}
用法:
override func viewDidLoad() {
super.viewDidLoad()
// Annotations
var annotation:MapPin
annotation.title = "test"
}