2016-09-15 23 views
5

Tôi đang sử dụng ứng dụng có Firebase.Lấy dữ liệu từ Firebase trong Swift 3.0

Sau khi chuyển đến nhanh 3,0 ngày hôm nay, Xcode hỏi tôi phải thay đổi mã này:

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

để mã này:

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

Nhưng trong dòng thứ hai nó mang lại cho tôi một lỗi:

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

Đây là dữ liệu json trong FireBase:

{ 
    “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

bạn có thể cung cấp JSON tối thiểu của bạn, dưới dạng văn bản không đoạn mã – Dravidian

+0

@Dravidian Chắc chắn, làm – Eliko

+0

Bạn có quản lý để có được một sửa chữa cho điều này? –

Trả lời

2

Đây là giải pháp phù hợp, sau khi sửa mã của tôi:

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

con trỏ của tôi không chuyển đến ref.observe (.childThêm vào, với: {snapshot in .... Xin hãy giúp! –

+0

@NupurSharma Hãy cho tôi xem mã của bạn – Eliko

3

Tôi chỉ cố định nó đúc snapshot.value as! [String:AnyObject]


EDIT:

Better giải pháp (sử dụng 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

Ok Tôi đã thử nó và nhận được cảnh báo trong dòng thứ 3: 'So sánh giá trị không tùy chọn kiểu '[String: AnyObject]' đến nil luôn trả về true'. Btw, nơi tôi đặt "chó"? – Eliko

1

Nó lấy tất cả người sử dụng dữ liệu căn cứ hỏa lực hiện có và các cửa hàng trong 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) 
Các vấn đề liên quan