具有第一个字符的NSPredicate过滤器数组

时间:2015-10-09 14:51:52

标签: ios swift filter nsarray nspredicate

我正在尝试使用名字的第一个字母

过滤数组

我正在使用它:

let predicate = NSPredicate(format: "name beginswith[c] %@", sections.objectAtIndex(section) as! String)
    let sectionArray = self.mContacts.filteredArrayUsingPredicate(predicate)

哪个部分是:

let sections = NSArray(objects: "#","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")

#必须包含以数字开头的所有名称。

但我不能用这种方法过滤数字开头的名字。

我正在寻找一些建议,谢谢你

1 个答案:

答案 0 :(得分:1)

我用MATCHES [c]

解决了这个问题
for sectionLetter in sections {
        if (sectionLetter as! String) == "#" {
            let predicateFormat = NSString(format: "name MATCHES[c] '[0-9].*'")
            let predicate:NSPredicate = NSPredicate(format:predicateFormat as String)

            let sectionArray = self.mContacts.filteredArrayUsingPredicate(predicate)
            mSectionArray.addObject(sectionArray)
        } else {
            let predicateFormat = NSString(format: "name MATCHES[c] '(%@).*'", sectionLetter as! String)
            let predicate:NSPredicate = NSPredicate(format:predicateFormat as String)

            let sectionArray = self.mContacts.filteredArrayUsingPredicate(predicate)
            mSectionArray.addObject(sectionArray)
        }
    }