致命错误火力地堡快照

问题描述:

每次我运行此我得到这个错误:致命错误:致命错误火力地堡快照

Unexpectedly found nil while unwrapping an Optional value 2017-09-15 06:30:04.650075+0200 RollerBank[845:211470] fatal error: unexpectedly found nil while unwrapping an Optional value.

我不知道我已经做错了。有人能帮助我吗?

func reload(){ 
      //get data 
      Database.database().reference().child("Rollerbanken").observe(.value, with: { (snapshot) in 



       for item in snapshot.children{ 

        if let itemDict = snapshot.value as? NSDictionary { 


         let annotation = MKPointAnnotation() 


         annotation.title = itemDict["TypeControle"] as! String 
         let tijd = itemDict["Tijd"] as! String 
         annotation.subtitle = "Geplaatst om \(tijd)" 


         let getLatitude = itemDict["Latitude"] as! Double 
         let getLongitude = itemDict["Longitude"] as! Double 

         annotation.coordinate = CLLocationCoordinate2D(latitude: getLatitude, longitude: getLongitude) 

         self.map.addAnnotation(annotation) 

        } 
       } 
      }) 
     } 

Structure

+0

ü可以贴上 “Rollerbanken” 的结构? – Appygix

+0

我有我的问题更新 –

使用它来分析从Firebase收到的值。

if let value = snapshot.value as? Dictionary<String, Any> { 

    for key in value.keys { 
      if let itemDict = value[key] as? Dictionary<String, AnyObject> { 

       let annotation = MKPointAnnotation() 
       annotation.title = itemDict["TypeControle"] as! String 
       let tijd = itemDict["Tijd"] as! String 
       annotation.subtitle = "Geplaatst om \(tijd)" 

       let getLatitude = itemDict["Latitude"] as? String 
       let getLongitude = itemDict["Longitude"] as? String 
       if let lat = getLatitude, let long = getLongitude { 
        annotation.coordinate = CLLocationCoordinate2D(latitude: Double(lat), longitude: Double(long)) 

        self.map.addAnnotation(annotation) 
      } 
     } 
    } 
} 
+0

我现在有一个不同的错误:无法将类型'__NSCFString'(0x1b2ce3f70)的值转换为'NSNumber' (0x1b2cf0b38)。 2017-09-15 07:14:30.180612 + 0200 RollerBank [898:224284]无法将类型'__NSCFString'(0x1b2ce3f70)的值转换为'NSNumber'(0x1b2cf0b38)。 –

+0

您会在哪一行收到此错误?同时检查你的dataType一次。 – Surjeet

+0

我认为当您尝试获取纬度,经度时,您会收到此次崩溃。根据你的数据库结构,它们是字符串值,而你将它们转换为Double。 – Surjeet

我相信你的问题是与这条线

if let itemDict = snapshot.value as? NSDictionary 

你让你的itemDict整个对象的字典。所以你没有“TypeControle”键,你有一个生成的Firebase ID的键。你可以通过child.value获得你正在寻找的字典,我相信。

if let itemDict = item.value as? NSDictionary 
+0

我得到一个错误..:类型'Any'的值没有成员'值' –

你的代码改成这样:

for item in snapshot.children { 

    let snapshotValue = item.value as! [String: AnyObject] 
    key = snapshotValue["TypeControle"] as! String 
    tijd = snapshotValue["tijd "] as! String 
    // etc.. 
} 

希望它可以帮助