2016-05-03 8 views
0
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 
     } 

    } 

エラー:暗黙的に生成されたsuper.init呼び出しでプロパティself.coordinateが初期化されませんでした。カスタムMKAnnotation辞書入力(CLLocationCoordinate2d)

これを解決する方法はありますか?

ありがとうございました。

+0

最初の行は「でsuper.init()」である必要があります[それはエラーのメッセージがあなたを言っているまさにである] –

+0

私はこの行を追加していますが、同じエラーを書き込みます。プロパティself.coordinateは初期化されていませんat super.init call –

+1

'coordinate'はオプションではないので、コントロールフローの各ブランチに対して初期化する必要があります。 – Moritz

答えて

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 
     } 
} 
関連する問題