2017-10-15 19 views
0

這裏是我的數據庫結構:從一個節點改變了一個孩子,另一個用於火力地堡斯威夫特

users 
    user1 
     nodeA 
     nodeToBeMoved 
      content : content1 
      date : date1 
    user2 
     nodeB 

我期待從節點A移動「nodeToBeMoved」到節點B,我試圖把content1和date1放入變量「value」中,然後刪除「nodeToBeMoved」,然後將這些變量添加到節點B.但由於某種原因,我的應用程序崩潰了。這裏是我的代碼:

self.ref.child("users").child(user1).child(nodeA).child(nodeToBeMoved).observe(.value, with: { (snapshot) in 
      let value = snapshot.valueInExportFormat() as AnyObject 
      self.ref.child(users).child(user1).child(nodeA).child(nodeToBeMoved).removeValue() 
      self.ref.child(users).child(user2).child(nodeB).child(nodeToBeMoved).child(content).setValue(value["content"] as Any) 
      self.ref.child(users).child(user2).child(nodeB).child(nodeToBeMoved).child(date).setValue(value["date"] as Any) 



     }) { (error) in 
      print(error.localizedDescription) 
     } 

我相信我有一個變量類型的問題。有人可以幫我解決這個問題嗎?而且這也是我的方法,但如果你知道一個更好的方法來做到這一點,可以隨意說出來。

+0

你與毀人有什麼錯誤? –

+0

致命錯誤:嘗試橋接隱含解包的可選包含無 – user8660271

+0

是的好舊的意外發現零。請在這裏搜索更多關於這個錯誤。很多答案 –

回答

0

試試這個`

self.ref.child("users").child(user1).child(nodeA).child(nodeToBeMoved).observe(.value, with: { (snapshot) in 
     let value = snapshot.value as NSdictionnary 
     if let actualvalue = value { 
     self.ref.child(users).child(user1).child(nodeA).child(nodeToBeMoved).removeValue() 
     self.ref.child(users).child(user2).child(nodeB).child(nodeToBeMoved).child(content).setValue(actualvalue["content"] as Any) 
     self.ref.child(users).child(user2).child(nodeB).child(nodeToBeMoved).child(date).setValue(actualvalue["date"] as Any) 

     } 




    }) { (error) in 
     print(error.localizedDescription) 
    } 
相關問題