gpt4 book ai didi

swift - 在 Picker Swift 上数组选择第一个数据

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

电阻阵列1阵列3阵列IM,我在这个收集。我想绘制这个数组的第一个元素。但当我这样做时,我在实践中遇到了错误。如何获取文本字段中选择器中的第一个数据?我想将文本字段打印到选择器中的第一个数据。该代码在此状态下不起作用。

我在这行代码中遇到错误:

 getDevice.text = gradePickerValues[0]
     override func viewDidLoad() {
super.viewDidLoad()
GetDevice()
getDevice.text = gradePickerValues[0]
}

var picker = UIPickerView()

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

let multiplearray = DispatchGroup()


@objc func GetDevice() {


if !chipnumber.text!.isEmpty {
multiplearray.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.multiplearray.leave()
})

}

if !chipnumber2.text!.isEmpty {
multiplearray.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.multiplearray.leave()
})

}

if !chipnumber3.text!.isEmpty {
multiplearray.enter()

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

ref.observeSingleEvent(of: .value, with: { (snapshot) in

let dic = snapshot.value as! String
self.gradePickerValues3 = [dic]
self.multiplearray.leave()
})

}

multiplearray.notify(queue:.main) {

self.gradePickerValues = self.gradePickerValues1 + self.gradePickerValues2 + self.gradePickerValues3

}
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
getDevice.text = gradePickerValues[row]

switch row {
case 0:
fetchDevies()

case 1:
fetchdevieschip2()

case 2:
fetchdevieschip3()

default: break

}
self.view.endEditing(true)
}

最佳答案

GetDevice() 包含异步任务。填充数组之后更新notify闭包中的标签。

并且请以小写字母开头命名函数/方法。

override func viewDidLoad() {
super.viewDidLoad()
getDevice()
}

...

@objc func getDevice() {

...

multiplearray.notify(queue:.main) {
self.gradePickerValues = self.gradePickerValues1 + self.gradePickerValues2 + self.gradePickerValues3
getDevice.text = gradePickerValues[0]
}

关于swift - 在 Picker Swift 上数组选择第一个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56726117/

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