Firebase Retrieve Data - Cannot Distinguish Value

Firstly, I checked these answers, which do not help me: Swift JSON error, could not pass the value of type '__NSArrayM' (0x507b58) to 'NSDictionary' (0x507d74)

Get data from Firebase

When retrieving data from Firebase (3.x), I get an error:

Could not cast value of type '__NSArrayM' (0x10ca9fc30) to 'NSDictionary' (0x10caa0108).

with this code and tree:

Wood:

enter image description here

Feature extraction:

func retrievePlanes() {

    print("Retrieve Planes")

    ref = FIRDatabase.database().reference(withPath: "results")

    ref.observe(.value, with: { snapshot in

        var newItems: [Planes] = []

        for item in snapshot.children {
            let planesItem = Planes(snapshot: item as! FIRDataSnapshot)
            newItems.append(planesItem)
        }

        self.planes = newItems
        self.tableView.reloadData()

    })

}

Planes.swift - data management

import Foundation
import Firebase
import FirebaseDatabase

struct Planes {

    let key: String!
    let name: String!
    let code:String!
    let flightRange: Int?
    let typicalSeats: Int?
    let maxSeats: Int?
    let wingSpan: String!
    let takeoffLength: Int?
    let rateClimb: Int?
    let maxCruiseAltitude: Int?
    let cruiseSpeed: String!
    let landingLength: Int?
    let engines: String!
    let votes: Int?
    let data: String!
    let imagePlane:String!
    let imageTakenFrom: String!
    let ref: FIRDatabaseReference?

    init(name: String, code: String, flightRange: Int, typicalSeats: Int, maxSeats: Int, wingSpan: String, takeoffLength: Int, rateClimb: Int, maxCruiseAltitude: Int, cruiseSpeed: String, landingLength: Int, engines: String, votes: Int, data: String, imagePlane: String, imageTakenFrom: String, key: String = "") {

        self.key = key
        self.name = name
        self.code = code
        self.flightRange = flightRange
        self.typicalSeats = typicalSeats
        self.maxSeats = maxSeats
        self.wingSpan = wingSpan
        self.takeoffLength = takeoffLength
        self.rateClimb = rateClimb
        self.maxCruiseAltitude = maxCruiseAltitude
        self.cruiseSpeed = cruiseSpeed
        self.landingLength = landingLength
        self.engines = engines
        self.votes = votes
        self.data = data
        self.imagePlane = imagePlane
        self.imageTakenFrom = imageTakenFrom
        self.ref = nil

    }

    init(snapshot: FIRDataSnapshot) {

        ref = snapshot.ref
        key = snapshot.key
        let snapshotValue = snapshot.value as! [String:AnyObject]
        name = snapshotValue["name"] as! String
        code = snapshotValue["code"] as! String
        flightRange = snapshotValue["intFlightRange"] as? Int
        typicalSeats = snapshotValue["intTypicalSeats"] as? Int
        maxSeats = snapshotValue["intMaxSeats"] as? Int
        wingSpan = snapshotValue["wingSpan"] as! String
        takeoffLength = snapshotValue["intTakeoffLength"] as? Int
        rateClimb = snapshotValue["intRateClimb"] as? Int
        maxCruiseAltitude = snapshotValue["intMaxCruiseAltitude"] as? Int
        cruiseSpeed = snapshotValue["cruiseSpeed"] as! String
        landingLength = snapshotValue["intLandingLength"] as? Int
        engines = snapshotValue["engines"] as! String
        votes = snapshotValue["votes"] as? Int
        data = snapshotValue["data"] as! String
        imagePlane = snapshotValue["planeImage"] as! String
        imageTakenFrom = snapshotValue["imageTakenFrom"] as! String
    }

in line: let snapshotValue = snapshot.value as! [String:AnyObject]

I believe this is due to the value of the snapshot, which cannot be found in [String:AnyObject]due to Intbelow. (It works when I only have Stringanother case).

I also know that Firebase "converts" the JSON tree to these objects [link] :

  • NSString
  • NSNumber
  • NSArray
  • NSDictionnary

, snapshot.value, .

.

EDIT: . . 2: . . .

+4
3

, . ( Firebase 2.x OS X Swift 3, )

:

  "what-am" : {
    "results" : [ {
      "code" : "738/B738",
      "data" : "Boeing",
      "engines" : "Rolls"
    }, {
      "code" : "727/B727",
      "data" : "Boeing",
      "engines" : "Pratt"
    } ]
  }

Planes struct

struct Planes {

    var code:String!
    var data: String!
    var engines: String!

    init(code: String, data: String, engines: String ) {

        self.code = code
        self.data = data
        self.engines = engines
    }

    init(snapshot: FDataSnapshot) {

        let snapshotValue = snapshot.value as! [String:AnyObject]

        code = snapshotValue["code"] as! String
        data = snapshotValue["data"] as! String
        engines = snapshotValue["engines"] as! String
    }
}

, , , .

let ref = self.myRootRef.child(byAppendingPath: "what-am/results")!

ref.observe(.value, with: { snapshot in

        if ( snapshot!.value is NSNull ) {
            print("not found")
        } else {

            var newItems: [Planes] = []

            for item in (snapshot?.children)! {
                let planesItem = Planes(snapshot: item as! FDataSnapshot)
                newItems.append(planesItem)
            }

            self.planes = newItems
            print(self.planes)

        }
})

, ,

[Swift_Firebase_Test.Planes(code: 738/B738, data: Boeing, engines: Rolls),
 Swift_Firebase_Test.Planes(code: 727/B727, data: Boeing, engines: Pratt)]

, Planes.

,

let snapshotValue = snapshot.value as! [String:AnyObject]

, : , String: AnyObject .

- Swift 3

for item in (snapshot?.children)!

, .

, , node. . ( )

 let ref = self.myRootRef.child(byAppendingPath: "what-am/results")!
 ref.observe(.value, with: { snapshot in
      if ( snapshot!.value is NSNull ) {
           print("not found")
      } else {
           for child in (snapshot?.children)! {
                let snap = child as! FDataSnapshot
                let dict = snap.value as! [String: String]
                let engines = dict["engines"]
                print(engines!)
           }    
      }
 })
+2

: -

  • :Int?, , :Int!

  • firebase - Int, let key: String!, let key: Int!

  • let snapshotValue = snapshot.value as! [AnyHashable:Any] ( 3)

init: -

let snapshotValue = snapshot.value as! [String:AnyObject]

let snapshotValue = (snapshot.value as! NSArray)[0] as! [String:AnyObject]
0

I think that you have an extra array in your key result value for firebase data.

  • You should try deleting this array or

  • You can get the dictionary from the first index of the array, for example:

    // .. your code
    let snapshotValue = (snapshot.value as! [AnyObject])[0] as! [String:AnyObject];
    // .. your code
    
0
source

Source: https://habr.com/ru/post/1657147/


All Articles