Resistor array 1 array 3 array IM, and I'm in this to collect. I want to draw the first element of this array. But when I do this, I get an error in practice. How do I get the first data in the picker in the textfield? I want to print textfield to the first data in the picker. The code does not work with this state.
I get an error in this line of code:
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()
contains asynchronous tasks. Update the label in the notify
closure after populating the array.
And please name functions/methods with starting lowercase letter.
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]
}