2016-05-03 12 views
0
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]] { 
     self.coordinate.latitude = coords.values.first!["latitude"]! 
     self.coordinate.longitude = coords.values.first!["longitude"]! 
    } 
    super.init() 
} 
/* 
init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D){ 
    self.title = title 
    self.subtitle = subtitle 
    self.coordinate = coordinate 

}*/ 

}リニューアルのInit(MKAnnotation)

。 Firebaseを使用し、データベースの結果が辞書にあるため、私はinitを再設計する必要があります。 それは書いている:プロパティself.coordinateだから暗黙的に生成さでsuper.initコール

答えて

0
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]] { 
     self.coordinate.latitude = coords.values.first!["latitude"]! 
     self.coordinate.longitude = coords.values.first!["longitude"]! 
    } 
    super.init() 
} 

で初期化されず、ここでの問題は、主にあなたがしかif以内にあなたのcoordinateプロパティを初期化するという事実にあります。 coordinateはオプションとしてマークされていないため、super.init()を呼び出す前にSwiftイニシャライザのルールに従って多くの値を指定してください。

if内でのみ初期化するため、ifが失敗し、初期化されないことがあります。これにより、インスタンスは完全に無効な状態になり、スウィフトイニシャライザのルールでは許可されず、the first safety checkに失敗します。

Safety check 1

A designated initializer must ensure that all of the properties introduced by its class are initialized before it delegates up to a superclass initializer.

あなたcoordinateオプションのインスタンス変数を作成し、あなたの他のインスタンス変数が好きなければなりませんでした。しかし、おそらく最も理にかなっているのは、id & titleもおそらく非オプションであり、これらのプロパティはすべておそらくletの定数でなければならないと主張します。このように

、私のようなあなたのクラスとその初期化子を書き換えたい:

class Point: NSObject, MKAnnotation { 

    let id: String 
    let title: String? 
    let coordinate: CLLocationCoordinate2D 
    let subtitle: String? 

    init?(id: String, dictionary: [String: AnyObject]){ 
     self.id = id 

     title = dictionary["title"] as? String 

     subtitle = dictionary["subtitle"] as? String 

     guard let 
      coordinates = dictionary["coordinates"] as? [String: [String: Double]], 
      latitude = coordinates.values.first?["latitude"], 
      longitude = coordinates.values.first?["longitude"] else { 

       return nil 
     } 

     coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 

     super.init() 
    } 
} 

今、あなたはfailable初期化子を持っています。辞書内の値が正しくない場合、イニシャライザはnilを返します。それ以外の場合は、値はすべて適切に初期化されます。

+0

タイトルはMKAnnotationプロトコルに従ってオプションでなければならず、座標はオプションではありません。 –

+0

更新された回答を参照してください。 – nhgrif

+0

それはあなたの質問が言っているものにも近くない。 – nhgrif