gpt4 book ai didi

ios - NSPredicate 创建

转载 作者:行者123 更新时间:2023-12-01 18:17:39 24 4
gpt4 key购买 nike

我应该如何创建一个 NSPredicate 来测试一个 NSString 是否以某种模式开头,比如“AAA”?
非常感谢你!
我试图阅读 Apple 的引用资料,但无法理解。

最佳答案

像这样:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] %@", @"AAA"];
如果你想从数组中找到所有匹配的字符串:
for(NSString *n in [myArray filteredArrayUsingPredicate:predicate]) {
NSLog(@"%@", n);
}
并回答您对“c”和“d”字符等的评论:

SELF is in this case NSString object

BEGINSWITH[] explanation not needed, I think.

"String comparisons are by default case and diacritic sensitive. You can modify an operator using the key characters c and d within square braces to specify case and diacritic insensitivity respectively" (quote from Apple's Predicates Programming Guide)

关于ios - NSPredicate 创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20160618/

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