使用Swift搜索基于搜索文本的数组

时间:2015-04-17 04:50:26

标签: ios xcode swift dictionary filter

我是swift的新手,我有一个新项目。我正在尝试搜索功能。我有一个字典数组,包含firstName和lastName作为键,我需要根据包含搜索字符串的firstName过滤掉人物

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

在目标C中,我可以轻松地使用

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

我也明白同样的谓词可以在Swift上完成。但我正在寻找一个等效的Swift,这样我就可以获得如何使用map和filter。任何帮助表示赞赏

4 个答案:

答案 0 :(得分:11)

使用"CONTAINS[c]"选项rangeOfString()可以实现与谓词运算符.CaseInsensitiveSearch对应的搜索:

let searchText = "MITH"

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

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

斯威夫特3:

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

答案 1 :(得分:1)

您可以通过先定义要过滤的函数进行过滤,或者只在过滤器中包含过滤器。这里有一段摘录,您可以将其粘贴到演示两种选项的游乐场中:

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/

答案 2 :(得分:0)

请尝试以下方式。

我已通过constant value您需要在dynamic value处传递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)

答案 3 :(得分:0)

尝试,

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)

您可以自由使用关闭。

let searchText = "John Sam"

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

或,

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