xcode 使用 Swift 根据搜索文本搜索数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29690581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:01:09  来源:igfitidea点击:

Search an array based on search text using Swift

iosxcodeswiftdictionaryfilter

提问by Nassif

I am a newbie to swift and i have a new project. I am trying to do a search feature. I have a array of dictionaries, containing firstName and lastName as keys, i need to filter out people based on the firstName which contains the search string

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

As in objective C i could easily do with

就像在目标 C 中一样,我可以轻松地使用

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

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

我也知道可以在 Swift 上完成相同的谓词。但我正在寻找等效的 Swift,以便我可以了解如何使用 map 和 filter。任何帮助表示赞赏

回答by Martin R

A search corresponding to the predicate operator "CONTAINS[c]"can be achieved with rangeOfString()with the .CaseInsensitiveSearchoption:

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

let searchText = "MITH"

let filtered = nameList.filter {
    return 
let filtered = nameList.filter {
    return 
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({
var result = (nameList.filter({
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)
["lastName"] == "Smith"})).filter({
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)
["firstName"] == "Johnny"})
["firstName"] == "Johnny"}) r var s = nameList.filter({
let searchText = "John Sam"

let results = nameList.filter {
    nameDictionary in
    nameDictionary["firstName"] == searchText
}
["lastName"] == "Smith"}) s
["firstName"]?.range(of: searchText, options: .caseInsensitive) != nil }
["firstName"]?.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil } // Result: [[firstName: Smith, lastName: Green]]

Swift 3:

斯威夫特 3:

let searchText = "John Sam"
    let results = nameList.filter {
        ##代码##["firstName"] == searchText
    }

回答by Derick

You can filter by defining a function to filter by first, or just include the filter in a closure. Here's an excerpt that you can paste into a playground that demonstrates both options:

您可以通过定义一个函数来过滤,或者只在闭包中包含过滤器。这是您可以粘贴到演示这两个选项的操场上的摘录:

##代码##

You can also filter the results of a filter to search for first and last name, e.g.

您还可以过滤过滤器的结果以搜索名字和姓氏,例如

##代码##

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

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

回答by Abhishek Sharma

Please try with following way.

请尝试以下方式。

I have passed constant valueyou need to pass your dynamic valueat place of someString variable.

我已经通过了constant value你需要通过你dynamic value的地方someString variable

##代码##

回答by bhavesh

Try,

尝试,

##代码##

You are free to use closure.

您可以自由使用闭包。

##代码##

or,

或者,

##代码##