2
private  var contactsWithSections = [[WPH]]()

WPH have following properties:

class WPH: NSObject {
    var fName: String? = nil
    var lName: String? = nil
    var number: String? = nil
    var email: String? = nil
}

I want to search based on fName i.e. in array have fname = alex, fname = alena, fname = flora

let searchStr = "al"

Then result should return objects for alex and alena

1

2 Answers 2

2

Found the more relevant solution that is a perfect match for my problem

let filtered = MasterList!.filter { (data) -> Bool in
            return data.fName?.range(of: Key, options: String.CompareOptions.caseInsensitive) != nil || data.lName?.range(of: Key, options: String.CompareOptions.caseInsensitive) != nil
        }
Sign up to request clarification or add additional context in comments.

Comments

0

You can just use flat map then Filter

FlatMap will join all section filter will filter it with your key

let results  =  contactsWithSections.flatMap {$0}.filter { (obj) -> Bool in
        guard let keyValue = obj.fName else  {return false}
        return keyValue == searchStr
    }

3 Comments

it will not work for me. private var contactsWithSections = [[WPH]]() if have sections and contents. I need to search all matches with let searchStr = "al" but your code will returns unique values.
flat map is working fine but not returning matched results.
this previous code should return matched result if condition ok , you will apply flatmap and after that will apply filter on flatted array see solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.