Swift:搜索对象数组

Abd*_*oor 3 arrays ios swift

我想在swift中搜索对象数组但我不知道如何:(

我试过了

filteredArrayUsingPredicate
Run Code Online (Sandbox Code Playgroud)

但仍然无法正常工作,它给了我一个错误信息

- 更新 -

错误信息是

swift:42:9: 'Array<search_options>' does not have a member named 'filteredArrayUsingPredicate'
Run Code Online (Sandbox Code Playgroud)

- 更新 -

class search_options {
        let id:String
        let option:String

        init(){}

        init(id:String ,option:String){
            self.id = id
            self.option = option
        }
    }
Run Code Online (Sandbox Code Playgroud)

我只想搜索选项变量

当我试图使用时

func searchBarSearchButtonClicked( searchBar: UISearchBar!)
{
    let filteredArray = filter(search_options_array) { $0 == "test" }
    println(searchBar.text)
}
Run Code Online (Sandbox Code Playgroud)

我收到了这条消息

swift:40:58: 'search_options' is not a subtype of 'String'
Run Code Online (Sandbox Code Playgroud)

flu*_*nic 11

查找特定对象的索引:

if let index = find(myArray, objectIAmLookingFor) {
    // found! do something
}
Run Code Online (Sandbox Code Playgroud)

过滤器阵列:

let filteredArray = filter(myArray) { $0 == objectIAmLookingFor }
Run Code Online (Sandbox Code Playgroud)

  • 如果你想使用`==`来比较`search_options`的两个实例,那么类(或结构)必须实现`Equatable`协议. (2认同)