字符串为 BEGINSWITH 特殊字符的过滤器对象的 NSPredicate

NSPredicate for filter objects whoes string is BEGINSWITH special characters

我有一种情况,我想过滤 NSArray 的对象,这些对象有 NSDictionary 个对象,每个对象都有键 name。我想过滤那些 name 键值应该以特殊字符或数字字符开头的对象,例如 0-9,~!@#$%^&*()_

我试图在 google 上找到它,但没有找到合适的解决方案。我使用了以下谓词但无法获得正确的对象。

NSPredicate *predicate=   [NSPredicate predicateWithFormat:@"self.name BEGINSWITH %@",@"[^0-9]+.*"];

BEGINSWITH 运算符不支持正则表达式。您可以使用以下谓词检查它是否以非字母字符开头:

 NSString *myRegex = @"[A-Za-z]*";
 NSPredicate *myTestPred = [NSPredicate predicateWithFormat:@"NOT (SELF MATCHES %@)", myRegex]