2016-09-15 97 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) 
相关问题