Firebase检索数据 - 无法投射价值 [英] Firebase Retrieve Data - Could not cast value
问题描述
检索功能:
func retrievePlanes(){
print(Retrieve Planes)
ref = FIRDatabase.database()。reference(withPath:results)
ref.observe(.value,with:{snapshot in
var newItems:[Planes] = []
为snapshot.children中的项目{
let planesItem = Planes(snapshot:item as!FIRDataSnapshot)
newItems.append(planesItem)
}
self.planes = newItems
self.tableView.reloadData()
))
}
$ c $ Planes.swift - 管理数据 <$ c $
$ c>导入基础
导入冷杉ebase
导入FirebaseDatabase
结构平面{
let key:String!
让名字:字符串!
让代码:字符串!
让flightRange:Int?
让typicalSeats:Int?
让maxSeats:Int?
让wingSpan:String!
让takeoffLength:Int?
让rateClimb:Int?
让maxCruiseAltitude:Int?
让cruiseSpeed:String!
让landingLength:Int?
让引擎:字符串!
让投票:诠释?
让data:String!
让imagePlane:String!
让imageTakenFrom:String!
让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。引擎=引擎
self.votes = votes
self.data = data
self.imagePlane = imagePlane
self.imageTakenFrom = ima geTakenFrom
self.ref = nil
$ b $ init(snapshot:FIRDataSnapshot){
ref = snapshot.ref
key = snapshot.key
let snapshotValue = snapshot.value as! [String:AnyObject]
name = snapshotValue [name] as!字符串
code = snapshotValue [code] as!字符串
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!字符串
landingLength = snapshotValue [intLandingLength] as? Int
engines = snapshotValue [engines] as!字符串
votes = snapshotValue [votes] as? Int
data = snapshotValue [data] as!字符串
imagePlane = snapshotValue [planeImage] as!字符串
imageTakenFrom = snapshotValue [imageTakenFrom] as!字符串
}
: let snapshotValue = snapshot。价值为! [String:AnyObject]
我想这是由于无法在 [ String:AnyObject]
,因为下面的 Int
。
(在另一种情况下,当我只有 String
时,它正在工作)。
我也知道Firebase将JSON树转换为这些对象 [link] :
-
NSString
-
NSNumber
-
NSArray
-
NSDictionnary
但我找不出什么
感谢您的帮助。
感谢您的帮助。编辑:我刚刚发送了一个故障排除请求。将张贴更新。
编辑2:看杰伊的答案。在我的情况下,这棵树是错误的。 解决方案
我拿你的代码,缩小了一下测试,它正在工作。 (注意在OS X和Swift 3上的Firebase 2.x,但代码类似)
Firebase结构:
what-am:{
results:[{
code:738 / B738,
data:波音,
engines:Rolls
},{
code:727 / B727,
data:Boeing,
engines:Pratt
}]
}
这里是Planes struct
struct Planes {
var code:String!
var data:String!
var engines:String!
$ b $ 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!字符串
data = snapshotValue [data] as!字符串
engines = snapshotValue [engines] as!字符串
}
}
然后读入两个平面的代码,然后打印数组。
let ref = self.myRootRef.child(byAppendingPath:what-am / results )!
$ b ref.observe(.value,其中:$快照
$ b $如果(snapshot!.value是NSNull){
print(not found)
} else {
var newItems:[Planes] = []
for(snapshot?.children)!{
let planesItem = Planes (snapshot:item as!FDataSnapshot)
newItems.append(planesItem)
}
self.planes = newItems
print(self.planes)
$ b $最后输出
[Swift_Firebase_Test.Planes(code:738 / B738,data:Boeing,engines:Rolls),
Swift_Firebase_Test.Planes(code:727 / B727,data:Boeing,engines:Pratt)]
钥匙和名字都是零,
您询问的行
let snapshotValue = snapshot.value as! [String:AnyObject]
是有效的,因为快照包含一系列键: AnyObject的工程。
这行改变了由于Swift 3
for (快照?。儿童)中的项目!
但除此之外,代码可以正常工作。
试试这个以确保你正在阅读正确的节点。这读取上述结构并打印出每个引擎类型。 (测试和工作) let ref = self.myRootRef.child(byAppendingPath:what-am / results)!
ref.observe(.value,其中:{snapshot in $ b $ if(snapshot!.value is NSNull){
print(not found)
} else {
$(let $ child $!)$ {
let snap = child as!FDataSnapshot
let dict = snap.value as![String:String]
let engines = dict [ engine]
print(engines!)
$ bFirst, I have checked these answers that do not help me :
Swift JSON error, Could not cast value of type '__NSArrayM' (0x507b58) to 'NSDictionary' (0x507d74)
When retrieving data from Firebase (3.x), I have an error that occurs which is :
Could not cast value of type '__NSArrayM' (0x10ca9fc30) to 'NSDictionary' (0x10caa0108).
with this code and tree :
Tree :
Retrieving function :
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 - To manage the data
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
}
on the line : let snapshotValue = snapshot.value as! [String:AnyObject]
I suppose that is due to the snapshot value that can't be retrieved under [String:AnyObject]
because of the Int
below.
(It is working when I only have String
in another case).
I also know that Firebase "converts" the JSON tree to these objects [link]:
NSString
NSNumber
NSArray
NSDictionnary
but I can't figure out what has to be changed in the snapshot.value line to make it work.
Thanks for your help.
EDIT : I just sent a troubleshooting request. Will post updates.
EDIT 2: See Jay's answer. In my case the tree was wrong.
解决方案 I took your code and shrunk it down a bit for testing, and it's working. (note Firebase 2.x on OS X and Swift 3 but the code is similar)
Firebase structure:
"what-am" : {
"results" : [ {
"code" : "738/B738",
"data" : "Boeing",
"engines" : "Rolls"
}, {
"code" : "727/B727",
"data" : "Boeing",
"engines" : "Pratt"
} ]
}
Here's the 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
}
}
and then the code that reads in two planes, populates and array and then prints the array.
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)
}
})
and finally the output
[Swift_Firebase_Test.Planes(code: 738/B738, data: Boeing, engines: Rolls),
Swift_Firebase_Test.Planes(code: 727/B727, data: Boeing, engines: Pratt)]
Key and name are nil as I removed then from the Planes structure.
The line you asked about
let snapshotValue = snapshot.value as! [String:AnyObject]
is valid as the snapshot contains a series of key:value pairs so String:AnyObject works.
This line changed due to Swift 3
for item in (snapshot?.children)!
but other than that, the code works.
Try this to ensure you are reading the correct node. This reads the above structure and prints out each engine type. (tested and works)
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!)
}
}
})
这篇关于Firebase检索数据 - 无法投射价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!