Search array based on search text using Swift

I am a newbie and I have a new project. I am trying to execute a search function. I have a set of dictionaries containing firstName and lastName as keys, I need to filter people based on firstName, which contains a search string

let nameList : [Dictionary<String,String>] = 
[["firstName" : "John Sam","lastName" : "Smith"],
["firstName" : "Johnny","lastName" : "Smith"],
["firstName" : "Albert","lastName" : "Smith"],
["firstName" : "Alby","lastName" : "Wright"],
["firstName" : "Smith","lastName" : "Green"]]

Like in lens C, I could easily do with

[NSPredicate predicatewithformat:"firstName contains[c] %@",searchText] 

I also understand that the same predicate can be done on Swift. but I'm looking for the Swift equivalent, so that I can get how to use the map and filter. Any help appreciated

+4
source share
4 answers

A search matching the predicate operator "CONTAINS[c]"can be achieved using rangeOfString()with the option .CaseInsensitiveSearch:

let searchText = "MITH"

let filtered = nameList.filter {
    return $0["firstName"]?.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil
}

// Result: [[firstName: Smith, lastName: Green]]

Swift 3:

let filtered = nameList.filter {
    return $0["firstName"]?.range(of: searchText, options: .caseInsensitive) != nil
}
+8

, , . , , :

let nameList : [Dictionary<String,String>] =
[["firstName" : "John Sam","lastName" : "Smith"],
    ["firstName" : "Johnny","lastName" : "Smith"],
    ["firstName" : "Albert","lastName" : "Smith"],
    ["firstName" : "Alby","lastName" : "Wright"],
    ["firstName" : "Smith","lastName" : "Green"]]

func fn (x: Dictionary<String,String>) -> Bool {
    return x["firstName"] == "Johnny"
}

nameList.filter(fn)

var r = nameList.filter({$0["firstName"] == "Johnny"})

r

var s = nameList.filter({$0["lastName"] == "Smith"})

s

, .

var result = (nameList.filter({$0["lastName"] == "Smith"})).filter({$0["firstName"] == "Johnny"})

: http://locomoviles.com/ios-tutorials/filtering-swift-array-dictionaries-object-property/

+1

Please try as follows.

I passed constant value, you need to transfer dynamic valueto the place someString variable.

let someString = "Alby"
        var arr:NSArray =
            [["firstName" : "John Sam","lastName" : "Smith"],
            ["firstName" : "Johnny","lastName" : "Smith"],
            ["firstName" : "Albert","lastName" : "Smith"],
            ["firstName" : "Alby","lastName" : "Wright"],
            ["firstName" : "Smith","lastName" : "Green"]]


        var pre:NSPredicate = NSPredicate(format: "firstName CONTAINS[c] %@", someString)!
        var result:NSArray = arr.filteredArrayUsingPredicate(pre)

        print(result)
0
source

Give it a try

let searchText = "John Sam"

func isMatch (nameDictionary: Dictionary<String, String>) -> Bool {
    if(nameDictionary["firstName"] == searchText) {
        return true
    }
    else {
        return false
    }

}

let results = nameList.filter(isMatch)

You can use closure.

let searchText = "John Sam"

let results = nameList.filter {
    nameDictionary in
    nameDictionary["firstName"] == searchText
}

or,

let searchText = "John Sam"
    let results = nameList.filter {
        $0["firstName"] == searchText
    }
0
source

All Articles