import MapKit
import UIKit
class Point: NSObject, MKAnnotation {
var id: String?
var title: String?
var coordinate: CLLocationCoordinate2D
var subtitle: String?
init(id: String, dictionary: Dictionary<String, AnyObject>){
self.id = id
if let title = dictionary["title"] as? String {
self.title = title
}
if let subtitle = dictionary["subtitle"] as? String {
self.subtitle = subtitle
}
if let coords = dictionary["coordinates"] as? [String:[String:Double]] {
var latitude = coords.values.first!["latitude"]!
var longitude = coords.values.first!["longitude"]!
var location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.coordinate = location
}
}
错误:属性self.coordinate未在隐式生成的super.init调用中初始化。
任何想法如何解决这个问题?
感谢您的一些建议。
答案 0 :(得分:0)
您应该在
的其他情况下指定默认值if let coords = dictionary["coordinates"] as? [String:[String:Double]] {
...
} else {
self.coordinate = <assign a default location>
}
或者
如果没有坐标,则不应创建对象。所以让你的init可选。
init?(id: String, dictionary: Dictionary<String, AnyObject>){
if let coords = dictionary["coordinates"] as? [String:[String:Double]] {
self.id = id
if let title = dictionary["title"] as? String {
self.title = title
}
if let subtitle = dictionary["subtitle"] as? String {
self.subtitle = subtitle
}
var latitude = coords.values.first!["latitude"]!
var longitude = coords.values.first!["longitude"]!
var location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.coordinate = location
} else {
return nil
}
}