0

本质上,我有一个 textField 按下时需要打开一个 UIPickerView 并带有来自 JSON 的选择

UIPickerView在 Swift 中选择 a 并从 JSON 创建数组时,我分别致力于触发 a UItextField,但在组合时遇到了一些麻烦。

对于JSON我使用 Almofire 仅仅是因为它简化了流程并且UIPickerView是通过编程方式编写的。

我正在使用的 JSON 如下所示:

[{“model”:”model1”},{“model":"model2”},
{“model":"model3”},{“model":"model4”},{“model":"model5”},{“model":"model6”}]

到目前为止,Almofire 看起来像这样:

        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)


            }
        }

textField使用以下代码触发打开 UIPickerView :

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]
    }

}

如何用 JSON 响应替换硬编码数组?

4

1 回答 1

0

你正在使用很多不好的做法

  • 不要在 Swift 中使用、 、NSURLNSData(用于JSON 数据)。使用本机类型。NSArrayNSDictionaryAnyObject
  • 不要.mutableContainers在 Swift 中使用。该选项毫无意义。省略options参数
  • Swift 变量名以小写字母开头。
  • 永远不要使用同步从远程 URL 加载数据Data(contentsOf。使用异步URLSession

最有效的解决方案是使用Decodable

在类之外声明结构

struct Model : Decodable {
    let model : String
}

将选择器源声明为变量并以复数形式

var models = [String]()

viewDidLoad末尾插入

let url = URL(string: "https://www.test.com/test/test")!
let dataTask = URLSession.shared.dataTask(with: url) { data, _, error in
    if let error = error { print(error); return }
    do { 
        let result = try JSONDecoder().decode([Model].self, from: data!)
        self.models = result.map{$0.model}
        DispatchQueue.main.async {
          self.pickerview.reloadAllComponents()
        }
    } catch { print(error) }
}
dataTask.resume()

即使有JSONSerialization(没有Model结构)它也很简单

let url = URL(string: "https://www.test.com/test/test")!
let dataTask = URLSession.shared.dataTask(with: url) { data, _, error in
    if let error = error { print(error); return }
    do { 
        if let result = try JSONSerialization.jsonObject(with: data!) as? [[String:String]]
            self.models = result.compactMap{$0["model"]}
            DispatchQueue.main.async {
              self.pickerview.reloadAllComponents()
            }
        }
    } catch { print(error) }
}
dataTask.resume()

选择器数据源方法是

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return models.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return models[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    TextField.text = models[row]
}

Alamofire对于简单的 GET 请求来说太过分了

于 2019-07-02T18:49:34.077 回答