gpt4 book ai didi

swift - 多字符串变量数组 swift

转载 作者:行者123 更新时间:2023-11-30 10:41:58 25 4
gpt4 key购买 nike

我有两个数组,我想将这些数组收集为一个数组。所以我想做的是拥有

gradepickervalues 1 + gradepickervalues 2 + gradepickervalues 3 = gradePickerValues

我该怎么办?它现在在代码中不起作用。

 var picker = UIPickerView()

var gradePickerValues1 = [String]()
var gradePickerValues2 = [String]()
var gradePickerValues3 = [String]()
var gradePickerValues = [String]()

@objc func GetDevice() {


if !chipnumber.text!.isEmpty {
let ref = Database.database().reference().child(chipnumber.text!).child("titles").child("0").child("DeviceName")

ref.observeSingleEvent(of: .value, with: { (snapshot) in
//let value: NSDictionary = snapshot.value as! NSDictionary
let dic = snapshot.value as! String
self.gradePickerValues = [dic]
})

}

if !chipnumber2.text!.isEmpty {
let ref = Database.database().reference().child(chipnumber2.text!).child("titles").child("0").child("DeviceName")

ref.observeSingleEvent(of: .value, with: { (snapshot) in
//let value: NSDictionary = snapshot.value as! NSDictionary
let dic = snapshot.value as! String
self.gradePickerValues = [dic]

})

}

if !chipnumber3.text!.isEmpty {
let ref = Database.database().reference().child(chipnumber3.text!).child("titles").child("0").child("DeviceName")

ref.observeSingleEvent(of: .value, with: { (snapshot) in
//let value: NSDictionary = snapshot.value as! NSDictionary
let dic = snapshot.value as! String
self.gradePickerValues = [dic]

})

}

}

最佳答案

目前您在每个响应中都覆盖了gradePickerValues,您可以尝试使用

var picker = UIPickerView() 
var gradePickerValues1 = [String]()
var gradePickerValues2 = [String]()
var gradePickerValues3 = [String]()
var gradePickerValues = [String]()

let g = DispatchGroup()

@objc func GetDevice() {



if !chipnumber.text!.isEmpty {

g.enter()

let ref = Database.database().reference().child(chipnumber.text!).child("titles").child("0").child("DeviceName")

ref.observeSingleEvent(of: .value, with: { (snapshot) in
//let value: NSDictionary = snapshot.value as! NSDictionary
let dic = snapshot.value as! String
self.gradePickerValues1 = [dic]
self.g.leave()
})

}

if !chipnumber2.text!.isEmpty {

g.enter()

let ref = Database.database().reference().child(chipnumber2.text!).child("titles").child("0").child("DeviceName")

ref.observeSingleEvent(of: .value, with: { (snapshot) in
//let value: NSDictionary = snapshot.value as! NSDictionary
let dic = snapshot.value as! String
self.gradePickerValues2 = [dic]
self.g.leave()
})

}

if !chipnumber3.text!.isEmpty {

g.enter()

let ref = Database.database().reference().child(chipnumber3.text!).child("titles").child("0").child("DeviceName")

ref.observeSingleEvent(of: .value, with: { (snapshot) in
//let value: NSDictionary = snapshot.value as! NSDictionary
let dic = snapshot.value as! String
self.gradePickerValues3 = [dic]
self.g.leave()

})

}

g.notify(queue:.main) {

self.gradePickerValues = gradePickerValues1 + gradePickerValues2 + gradePickerValues3

print(self.gradePickerValues)
}

}

关于swift - 多字符串变量数组 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56598337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com