gpt4 book ai didi

ios - 带有第一个字符的 NSPredicate 过滤器数组

转载 作者:行者123 更新时间:2023-11-29 01:35:10 25 4
gpt4 key购买 nike

我正在尝试用名字的第一个字母过滤一个数组

我正在使用这个:

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

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

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

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

最佳答案

我用 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)
}
}

关于ios - 带有第一个字符的 NSPredicate 过滤器数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33041306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com