My project has a UIPickerView and I need to return the count of an array for numberOfRowsInComponent.
Heres my code so far:
Heres my code so far:
-
Code:
import UIKit import CloudKit class SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { var db:CKDatabase? var currentRecords:[CKRecord] = [] var currentRecordsInt:[Int] = [] var numb: Int = 0 var numbReturn: Int = 0 @IBOutlet weak var pickCar: UIPickerView! override func viewDidLoad() { self.pickCar.delegate = self self.pickCar.dataSource = self db = CKContainer.default().privateCloudDatabase super.viewDidLoad() } func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func getCarArrayNumb() -> Int { let predicate = NSPredicate(value: true) let query = CKQuery(recordType: "mpgTracker", predicate: predicate) db?.perform(query, inZoneWith: nil, completionHandler: { (records:[CKRecord]?, e:Error?) in if e != nil { return } self.currentRecords = records! var record: CKRecord = records![0] print("getCarArrayNumb record.allKeys() \(record.allKeys())") // OK, expected array var counts: [String: Int] = [:] var numb: Int = 0 func countNumb() -> Int { for item in record.allKeys() { counts[item] = (counts[item] ?? 0) + 1 numb = numb + 1 } print("return numb: \(numb)") // 13 return numb } print("countNumb: \(countNumb())") // 13 }) return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return getCarArrayNumb() } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return "" }
I'm sure the answer is simple but so far it alludes me.
Thanks for any help!!
~paul