Firebase和swiftyJSON解析 [英] Firebase and swiftyJSON parsing
问题描述
一个个人ID的 imageName
,以及所有ID均为 imageName
s。 JSON结构:
检索JSON的代码罚款,但不显示 imageName
值:
ref.observe(FIRDataEventType .value){
(snapshot:FIRDataSnapshot!)in
let json = JSON(snapshot.value)
// print(JSON:\(json))
//打印IMAGENAME - 不工作
if imageName = json [12345] [0] [imageName]。string {
print(imageName:\( imageName))
}
// //打印IMAGENAME - 不工作
让theKeys = json.dictionary!.keys
theKeys {
let imageName = json [0] [0] [imageName]
print(imageName:\(imageName))
}
}
我的最终结果是在一个CollectionView中显示一个imageURL。看起来很接近,只是没有得到正确的swiftyJSON格式。首先,我想你应该解析你的数据的'firebase的方式'我想你可以调用它,而不是使用SwiftJSON 。这是我如何做'firebase的方式'
导入FirebaseDatabase
类ViewController:UIViewController {
var ref:FIRDatabaseReference!
$ b $重写fun viewDidLoad(){
super.viewDidLoad()
ref = FIRDatabase.database()。reference()
$ b $ func retrieveData(){
ref.child(12345)。observe(.childAdded,with {(snapshot)in
让dict = snapshot.value as?[String:AnyObject]
imageNamed = dict![imageName] as?String
print(\\ \\(imageNamed))
}
}
}
上面的代码适合我在SwiftJSON中使用
,我不是100%确定这是否可行,但也许我们可以试试$ / $>
让json = JSON(snapshot.value)as?[String:AnyObject]
if imageName = json! 12345] [0] [imageName]。string {
}
让theKeys = json.dictionary!.keys
在$ {$ b $ let imageName = json [0] [0] [imageName]
print(imageName:\(imageName))
}
swiftyJSON looks great for parsing Firebase. What would like to end up with is 2 possibilities:
An imageName
for one individual ID, and a list of imageName
s for all IDs. JSON Struture:
Code that retrieves JSON fine, but does not display imageName
values:
ref.observe(FIRDataEventType.value) {
(snapshot: FIRDataSnapshot!) in
let json = JSON(snapshot.value)
// print("JSON: \(json)")
// PRINT IMAGENAME - NOT WORKING
if let imageName = json[12345][0]["imageName"].string {
print("imageName: \(imageName)")
}
// // PRINT IMAGENAME - NOT WORKING
let theKeys = json.dictionary!.keys
for key in theKeys {
let imageName = json[0][0]["imageName"]
print("imageName: \(imageName)")
}
}
My end result is to end up with an imageURL to display in a CollectionView. It seems close, just not getting the swiftyJSON format correct. Thanks.
First I think you should parse your data the 'firebase way' I guess you can call it instead of using SwiftJSON. This is how I would do it the 'firebase way'
import FirebaseDatabase
class ViewController: UIViewController {
var ref: FIRDatabaseReference!
override fun viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
}
func retrieveData() {
ref.child("12345").observe(.childAdded, with { (snapshot) in
let dict = snapshot.value as? [String: AnyObject]
let imageNamed = dict!["imageName"] as? String
print("\(imageNamed)")
}
}
}
The code above works well for me
in SwiftJSON, I'm not 100% sure if this will work but maybe we could try
let json = JSON(snapshot.value) as? [String: AnyObject]
if let imageName = json!["12345"][0]["imageName"].string {
}
let theKeys = json.dictionary!.keys
for key in theKeys {
let imageName = json[0][0]["imageName"]
print("imageName: \(imageName)")
}
这篇关于Firebase和swiftyJSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!