I am trying to implement two side by side UIPickerViews, one for Type and the other for Subtype. Each is an array of strings
var typePickerData: [String] = [String]()
var subtypePickerData: [String] = [String]()
Each is a pretty simple array of names:
typePickerData = ["Monitor","Keyboard","Mouse"]
When the viewDidLoad fires or when a new Type is selected, the app makes a remote db call and I get a response containing JSON of subtype names which I want to use to populate and reload the subtype picker.
I am stuck on converting the response into subtypePickerData
let decoder = JSONDecoder()
if let jsonResponse = try? decoder.decode([String].self, from: data) {
print("parse subtype response \(jsonResponse)")
subtypePickerData = jsonResponse
DispatchQueue.main.async {
self.subtypePicker.reloadAllComponents()
}
}
What am I doing wrong here converting the JSON response to subtypePickerData?
this is what I am getting from the remote call
result Optional(<__NSArrayM 0x281370e70>( { name = Monitor; },{ name = "AV Accessories"; },{ name = Computer; },{ name = "Monitor Stands"; },{ name = "Bracket/Mount"; },{ name = Screen; }
Here is my updated code after solving issue
let decoder = JSONDecoder()
if let jsonResponse = try? decoder.decode(Subtypes.self, from: data) {
SubtypeList = jsonResponse.result
self.subtypePickerData = SubtypeList.map{$0.Name}
DispatchQueue.main.async {
self.subtypePicker.reloadAllComponents()
}
}
jsonResponsenil? Whytry?and not a properdo/try/catchto see if there is an error? And what's the JSON looking like?[String], just replacetry?withtry!you'll see...