Entweder sollten Sie einen Standardwert in anderem Fall von
if let coords = dictionary["coordinates"] as? [String:[String:Double]] {
...
} else {
self.coordinate = <assign a default location>
}
Oder
zuordnen Sie sollten nicht Objekt, wenn keine Koordinaten erstellen. Also machen Sie Ihre Init optional.
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
}
}
erste Zeile Ihres benutzerdefinierten Konstruktor sollte „super.init()“ sein [das ist genau das, was Botschaft Fehler Sie sagt] –
ich diese Linie bin hinzugefügt, aber es schreibt gleichen Fehler: Objekt self.coordinate nicht initialisiert bei super.init call –
Weil 'coordinate' kein Optional ist, muss es für jeden Zweig des Kontrollflusses initialisiert werden. – Moritz