Muju
Muju

Reputation: 979

how to sort two array and add it to third one using textfield in swift

I am new in swift and I am facing problem to search one by one. I tried this code

let appln_id = "800016"

let result = (dict["listAarry"] as? [[String:Any]])?.filter({ ($0["appln_id"] as? String) == appln_id })

but I want to search it in textfield

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{


    if textField == txtSearch{
        let certificationId = txtSearch.text //"800016"

        let result = (searchdict["listAarry"] as? [[String:Any]])?.filter({ ($0["appln_id"] as? String) == certificationId })
    }

    return true
}

enter image description here

Upvotes: 0

Views: 118

Answers (3)

Muju
Muju

Reputation: 979

I am so stupid I just has to use contains instead of ==

txtSearch.addTarget(self, action: #selector(AdmissionReportVC.textFieldDidChange(_:)), for: UIControl.Event.editingChanged)

 @objc func textFieldDidChange(_ textField: UITextField) { 
     var certificationId = String()
     certificationId =  txtSearch.text ?? ""
     print(certificationId)
     let result = (searchdict["listAarry"] as? [[String:Any]])?.filter({ 
     (($0["appln_id"] as? String)?.contains(certificationId) ?? false)})
     print("Result = ",result as Any
     SearchAny = result!
     tblView.reloadData()
 }

Upvotes: 1

Vollan
Vollan

Reputation: 1915

So you are currently calling the delegate function of shouldChangeCharactersIn on the textField. However, what you want to do is call:

@objc func textFieldValueChanged(_ textField: UITextField)

Along with:

searchTextField.addTarget(self, action: #selector(textFieldValueChanged), for: .editingChanged)

in the valueChanged method you should do your filter but some minor editing and then put that into your datasource for tableView and run your tableView.reloadData()

let result = (va["listAarry"] as? [[String:Any]])?.filter({ (($0["appln_id"] as? String)?.contains("textInput") ?? false)})

Upvotes: 0

Muhammad Awais
Muhammad Awais

Reputation: 340

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let previousText:NSString = textField.text! as NSString
    let updatedText = previousText.replacingCharacters(in: range, with: string)
    let result = dict.filter { (dict) -> Bool in
        if let app_id = dict["appln_id"] as? String {
            return app_id.contains(updatedText)
        }
        return false
    }

    return true
}

result will hold valued on the basis of searched term.

Upvotes: 0

Related Questions