从数组过滤数据

时间:2019-01-17 04:11:14

标签: ios arrays swift xcode uipickerview

我有一个用于UIViewController的类,它使用http获取一些数据。

class NCHINViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var RequestTokenBTN: UIButton!]
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var listIdTF: UITextField!
    @IBOutlet weak var listNameTF: UITextField!

    //set pickerView
    var PDVListId: [clsSpinnerNCHIN] = []
    var arrayMissingLI: [String] = [String]()
    var arrayMissingLN: [String] = [String]()

    override func viewDidLoad() {
        ...
    }

    override func didReceiveMemoryWarning() {
        ...
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        pickerView.subviews.forEach({
            $0.isHidden = $0.frame.height < 1.0
        })
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        //return pickerData.count
        if pickerView == listNameTF.inputView{
            return PDVListId.count
        } else {
            return PDVListName.count
        }
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        //return pickerData[row]
        if pickerView == listNameTF.inputView{
            return PDVListId[row].listName
        } else {
            return PDVListName[row]
        }
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if pickerView == listNameTF.inputView{
            return listNameTF.text = PDVListId[row].listName
        } else {
            return listIdTF.text = PDVListName[row]
        }
    }

    func reqNomSpinnerNCHIN(){
        NetworkingService.shared.reqSpinnerNominalCashin()  { spinnerResponse in
            if (spinnerResponse == nil) {
                self.view.makeToast("Technical problem, please try again...")
            } else {

                if (spinnerResponse?.success == Const.ResponseKey.Success) {
                    print(spinnerResponse ?? "")
                    self.PDVListId = (spinnerResponse?.data)!
                } else {

                    let popup = PopupDialog(title: "Failed", message: spinnerResponse?.error)
                    let buttonOK = DefaultButton(title: "OK") {

                    }
                    popup.addButton(buttonOK)
                    self.present(popup,animated: true,completion: nil)
                }
            }
        }
    }
}

spinnerResponse?.dataclsSpinnerNCHIN

在这种情况下,我想过滤PDVListID中的数据,这些数据是从spinnerResponse.data插入的数据

例如: 如果我从服务器获得的响应是​​

    T = In
    LI = 001
    LN = CG1

    T = Out
    LI = 001
    LN = CG2

    T = Missing
    LI = 001
    LN = CG3

    T = Out
    LI = 002
    LN = CG4

    T = In
    LI = 002
    LN = CG5

这就是我想要做的

if userData.type == "Missing" {
    //put all data from "LI" that "T == Missing" that i got from `spinnerResponse?.data` and save it into arrayMissingLI
    //put all data from "LN" that 'T == Missing" that i got from `spinnerResponse?.data` and save it into arrayMissingLN
}

如何选择T中的每一个并将LI放入arrayMissingLNarrayMissingLI中,并在PickerView中使用它来显示数据(如果我选择了Missing) ,输出和输入

1 个答案:

答案 0 :(得分:0)

 if let apiResponseData = spinnerResponse.data{

        arrayMissingLI.removeAll()
        arrayMissingLN.removeAll()

        _ = apiResponseData.filter { (dic) -> Bool in
            if dic.T == "Missing"{
                arrayMissingLI.append(dic.LI)
                arrayMissingLN.append(dic.LN)
            }
        }

    }

尝试一下。这将为您提供arrayMissingLI和arrayMissingLN的数组。 如果您有任何疑问,请告诉我。