本文介绍了将Firebase数据附加到Swift ios中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道我做错了什么!我只是想从Firebase数据库获取数据,并对每个结果进行迭代并将其放入数组中。我尝试了不同的方法。但是他们都返回一次一个结果到数组的结果。我有如下的数据: {
1400001:{
HHtype:3,
Sesgrup:4,
lg_Etharcaması:0,
lg_Total Harcama:3.11
},
1400002:{
HHtype:1,
Sesgrup:4,
lg_Etharcaması:1.2,
lg_Total Harcama:3.15
},
1400004 :
HHtype:3,
Sesgrup:4,
lg_Etharcaması:1.18,
lg_Total Harcama:3.06
},
1400005:{
HHtype:4,
Sesgrup:3,
lg_Etharcaması:2.21,
lg_Total Harcama:3.74
},
.......
而且,我正在使用一个代码如下:
func sen(){
var rootRef:FIRDatabaseReference!
var refHandle:UInt!
var haneRef:FIRDatabaseReference!
var numbersArray = [Int]()
rootRef = FIRDatabase.database()。reference()
haneRef = rootRef.child( )
haneRef.queryOrderedByChild(lg_Etharcaması)。queryLimitedToLast(4).observeEventType(.ChildAdded,withBlock:{(snapshot)in
let hhtype = snapshot .value![HHtype] as?Int
numbersArray.append(hhtype!)
print(numbersArray)
$ b $)
$结果是:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' 4,4]
我只想得到最后一个数组:[3,1,4,4 ]
我该怎么做?谢谢
解决方案
func sen(){
var rootRef:FIRDatabaseReference!
var refHandle:UInt!
var haneRef:FIRDatabaseReference!
var numbersArray = [Int]()
rootRef = FIRDatabase.database()。reference()
haneRef = rootRef.child( SES2014)
let queryCount = 4
haneRef.queryOrderedByChild(lg_Etharcaması)。queryLimitedToLast(queryCount).observeEventType(.ChildAdded,withBlock:{(snapshot)in
让hhtype = snapshot.value![HHtype] as?Int
numbersArray.append(hhtype!)
queryCount - = 1
if queryCount< = 0 {
print (numbersArray)
}
$ b})
}
I don't know what I am doing wrong! I just simply want to take data from Firebase database and iterate for each result and put them into an array. I have tried serval different approaches. But they all return results adding one at a time to the array. I have data like below:
{
"1400001" : {
"HHtype" : 3,
"Sesgrup" : 4,
"lg_Et harcaması" : 0,
"lg_Total Harcama" : 3.11
},
"1400002" : {
"HHtype" : 1,
"Sesgrup" : 4,
"lg_Et harcaması" : 1.2,
"lg_Total Harcama" : 3.15
},
"1400004" : {
"HHtype" : 3,
"Sesgrup" : 4,
"lg_Et harcaması" : 1.18,
"lg_Total Harcama" : 3.06
},
"1400005" : {
"HHtype" : 4,
"Sesgrup" : 3,
"lg_Et harcaması" : 2.21,
"lg_Total Harcama" : 3.74
},
.......
And, I am using a code that is below :
func sen(){
var rootRef: FIRDatabaseReference!
var refHandle: UInt!
var haneRef: FIRDatabaseReference!
var numbersArray = [Int] ()
rootRef = FIRDatabase.database().reference()
haneRef = rootRef.child("SES2014")
haneRef.queryOrderedByChild("lg_Et harcaması").queryLimitedToLast(4).observeEventType(.ChildAdded, withBlock: { (snapshot) in
let hhtype = snapshot.value!["HHtype"] as? Int
numbersArray.append(hhtype!)
print(numbersArray)
})
}
And, the result is:
[3]
[3, 1]
[3, 1, 4]
[3, 1, 4, 4]
I want to get only the last array: [3, 1, 4, 4]
How can I do this ? Thanks
解决方案
func sen(){
var rootRef: FIRDatabaseReference!
var refHandle: UInt!
var haneRef: FIRDatabaseReference!
var numbersArray = [Int] ()
rootRef = FIRDatabase.database().reference()
haneRef = rootRef.child("SES2014")
let queryCount = 4
haneRef.queryOrderedByChild("lg_Et harcaması").queryLimitedToLast(queryCount).observeEventType(.ChildAdded, withBlock: { (snapshot) in
let hhtype = snapshot.value!["HHtype"] as? Int
numbersArray.append(hhtype!)
queryCount -= 1
if queryCount <= 0 {
print(numbersArray)
}
})
}
这篇关于将Firebase数据附加到Swift ios中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!