Essentially I have a textField that when pressed needs to open a UIPickerView with a selection that comes from JSON
I have separately worked on triggering a UIPickerView when selecting a UItextField and creating arrays from JSON in Swift but am having some trouble putting together.
For the JSON I am using Almofire simply because it simplifies the process
and the UIPickerView is written programmatically.
The JSON I am working with looks like this:
[{“model”:”model1”},{“model":"model2”},
{“model":"model3”},{“model":"model4”},{“model":"model5”},{“model":"model6”}]
The Almofire so far looks like this:
let url = NSURL(string: "https://www.test.com/test/test")
let data = NSData(contentsOf: url! as URL)
var tmpValues = try! JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
tmpValues = tmpValues as NSArray
reloadInputViews()
for candidate in tmpValues {
if let cdict = candidate as? NSDictionary {
//model is the column name in sql/json
let model = cdict["model"]
self.values.append(model! as AnyObject)
}
}
Triggering the textField to open a UIPickerView is done using the following code:
import UIKit
class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
@IBOutlet weak var TextField: UITextField!
let model = ["model1","model2"]
var pickerview = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
TextField.inputView = pickerview
TextField.textAlignment = .center
TextField.placeholder = "Select Your Model"
pickerview.delegate = self
pickerview.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Names.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return Names[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
TextField.text = Names[row]
}
}
How can I replace the hard-coded array with the JSON response?
You are using a lot of bad practices
NSURL,NSData,NSArray,NSDictionaryandAnyObject(for JSON data) in Swift. Use native types..mutableContainersin Swift. The option is pointless. Omit theoptionsparameterData(contentsOf. Use asynchronousURLSessionThe most efficient solution is to decode the JSON with
DecodableOutside of the class declare the struct
Declare the picker source as variable and in plural form
In
viewDidLoadinsert at the endEven with
JSONSerialization(without theModelstruct) it's pretty straightforwardThe picker data source methods are
Alamofireis overkill for a simple GET request