2016-09-15 108 views
5

我正在使用Firebase的應用程序。從Swift 3.0的Firebase中獲取數據

今天3.0移動迅速後,Xcode的問我這個代碼更改:

ref.observeEventType(.ChildAdded, withBlock: { snapshot in 
      let currentData = snapshot.value!.objectForKey("Dogs") 
      if currentData != nil { 
      let mylat = (currentData!["latitude"])! as! [String] 
      let mylat2 = Double((mylat[0])) 
      let mylon = (currentData!["longitude"])! as! [String] 
      let mylon2 = Double((mylon[0])) 
      let userid = (currentData!["User"])! as! [String] 
      let userid2 = userid[0] 
      let otherloc = CLLocation(latitude: mylat2!, longitude: mylon2!) 
      self.distanceBetweenTwoLocations(self.currentLocation, destination: otherloc, userid: userid2) 
      } 
     }) 

這個代碼:

ref.observe(.childAdded, with: { snapshot in 
      let currentData = (snapshot.value! as AnyObject).object("Dogs") 
      if currentData != nil { 
       let mylat = (currentData!["latitude"])! as! [String] 
       let mylat2 = Double((mylat[0])) 
       let mylon = (currentData!["longitude"])! as! [String] 
       let mylon2 = Double((mylon[0])) 
       let userid = (currentData!["User"])! as! [String] 
       let userid2 = userid[0] 
       let otherloc = CLLocation(latitude: mylat2!, longitude: mylon2!) 
       self.distanceBetweenTwoLocations(self.currentLocation, destination: otherloc, userid: userid2) 
      } 
     }) 

但在第二行它給了我一個錯誤:

Cannot call value of non-function type 'Any?!'

這是FireBase中的json數據:

{ 
    「user1」 : { 
    "Dogs" : { 
     "User" : [ "siACmQZ7MDclDSO3hrCOp953kfl2" ], 
     "latitude" : [ "32.172344" ], 
     "longitude" : [ "34.858068" ] 
    } 
    「user2」 : { 
    "Dogs" : { 
     "User" : [ "siACmQZ7MDclDSO3hrCOp953kfl2" ], 
     "latitude" : [ "32.172344" ], 
     "longitude" : [ "34.858068" ] 
    } 
    「user3」 : { 
    "Dogs" : { 
     "User" : [ "siACmQZ7MDclDSO3hrCOp953kfl2" ], 
     "latitude" : [ "32.172344" ], 
     "longitude" : [ "34.858068" ] 
    } 
} 
+0

你能提供你的最小JSON,文本不片斷 – Dravidian

+0

@Dravidian當然,做 – Eliko

+0

你有沒有設法獲得此修復? –

回答

2

這是正確的解決方案,修復我的代碼之後:

ref.observe(.childAdded, with: { snapshot in 
      if let snapshotValue = snapshot.value as? [String:Any], 
       let currentData = snapshotValue["Dogs"] as? [String:Any] { 
       let userid = (currentData["User"])! as! [String] 
       let userid2 = userid[0] 
       let mylat = currentData["latitude"] as! [String] 
       let mylat2 = Double((mylat[0])) 
       let mylon = (currentData["longitude"])! as! [String] 
       let mylon2 = Double((mylon[0])) 
       let otherloc = CLLocation(latitude: mylat2!, longitude: mylon2!) 
       self.distanceBetweenTwoLocations(self.currentLocation, destination: otherloc, userid: userid2) 
      } 
+0

我的光標不會去ref.observe(.childAdded,與:{快照在....請幫助! –

+0

@NupurSharma請給我你的代碼請 – Eliko

3

我只是固定它鑄造snapshot.value as! [String:AnyObject]


編輯:

(使用SwiftyJSON)

更好的解決方案:

func parseFirebaseResponse(snapshot: FIRDataSnapshot) -> [JSON] { 
    var returnJSON: [JSON] = [] 
    let snapDict = snapshot.value as? [String:AnyObject] 
    for snap in snapshot.children.allObjects as! [FIRDataSnapshot] { 
     let json = snapDict?[snap.key] 
     returnJSON.append(JSON(json as Any)) 
    } 
    return returnJSON 
} 
+0

好吧,我試過了,我在第三行中得到一個警告:'比較類型'[String:AnyObject]的非可選值'nil總是返回true'。順便說一句,我在哪裏放「狗」? – Eliko

1

據檢索NSDictionary的所有現有火力用戶數據並存儲:

let ref = FIRDatabase.database().reference(fromURL: "DATABASE URL") 
let usersRef = ref.child("users").observeSingleEvent(of: .value, with: {(snapshot) in 
print(snapshot) 
let Dict = snapshot.value as! NSDictionary //stores users data in dictionary(key/value pairs) 
print(Dict) 
相關問題