2016-09-15 22 views
5

私はFirebaseでアプリを使用しています。Swift 3.0のFirebaseからのデータ取得

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) 
      } 
     }) 

しかし、2行目に、それは私にエラーを与える:このコードに

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) 
      } 
     }) 

3.0今日迅速に移動した後、Xcodeは、このコードを変更するために私に尋ねた

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

あなたは、このための修正を得ることができなかった – Dravidian

+0

@Dravidian確かスニペットないテキストは、 – Eliko

+0

を行ったようにあなたは、あなたの最小限のJSONを提供することはできますか? –

答えて

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、with:{snapshot in ....}助けてください! –

+0

@NupurSharmaコードを表示してください – Eliko

3

私はちょうどsnapshot.value as! [String:AnyObject]


EDITキャストそれを修正:(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

私はそれを試して、3行目に警告が表示されました。 'タイプ '[String:AnyObject]'の非オプション値をnilに比較すると常にtrueが返されます。 Btw、どこに「犬」を置くのですか? – Eliko

1

それ取得NSDictionaryの内のすべての既存firebaseのユーザーデータを格納:

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