简体   繁体   English

如何从nsarray的nsarray搜索特定联系人

[英]How to search particular contact from nsarray of nsarray

I have store the contact in alphabetical order so I have array of array for contact list which i have display on screen. 我已按字母顺序存储联系人,因此我在屏幕上显示了联系人列表数组。 Now i want to search the contact via name but predicate not work properly here. 现在,我想通过姓名搜索联系人,但在这里谓词无法正常工作。 I have done below code. 我已经完成了下面的代码。

   filterArray.filterUsingPredicate(NSPredicate(format: "ANY SELF.name CONTAINS[cd] '\(tfSearchBar.text!)'", argumentArray: nil))

In filter array first i have all contact but when i search for "a" it gives all the section array which has "a" in the contact name. 首先在过滤器数组中,我拥有所有联系人,但是当我搜索“ a”时,它将给出所有在联系人姓名中具有“ a”的节数组。 but here i have stuck. 但是我在这里卡住了。 It is not necessary that the all contact of the section contain "a" in the contact name. 该部分的所有联系人不必在联系人名称中包含“ a”。

For example 例如

(
    A:(
        {
            name = "abc"
            number = "123456"
        }
        {
            name = "azx"
            number = "123456"
        }
      )
)

For example for above example after search for "a". 例如,上面的示例在搜索“ a”之后。 when i search for "ab" then same array return by predicate.Not only first object. 当我搜索“ ab”时,谓词将返回相同的数组。不仅是第一个对象。 Any way to find only first object with out nested predicate. 以任何方式查找没有嵌套谓词的第一个对象。

I'm not sure what you are trying to accomplish with the word "ANY" in your predicate but it seems redundant. 我不确定您要在谓词中使用“ ANY”一词来完成什么,但这似乎是多余的。 This code works as you describe when put into a Playground: 当您将其放入Playground时,此代码的工作方式如您所述:

import UIKit

@objc class DataElement : NSObject {
    let name : String
    let number : String

    init(name : String, number : String) {
        self.name = name
        self.number = number
    }
}

let dataArray : NSArray = [
    DataElement(name: "abc", number: "123456"),
    DataElement(name: "azx", number: "689101")
]

let searchTerm = "a"
let predicate = NSPredicate(format: "SELF.name CONTAINS[cd] '\(searchTerm)'", argumentArray: nil)

dataArray.filteredArrayUsingPredicate(predicate)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM