我想过滤数组,这里的名字以非字母字符开头。我想在表视图的不同部分显示firstName以非字母字符开头的联系人。我尝试了下面的代码,但是它崩溃了,请在下面找到原因:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT(firstName BEGINSWITH[c] %@)",arrIndex]; //where arrIndex is the array of alphabetical characeters.
NSArray *arrContacts = [arrayTotalContacts filteredArrayUsingPredicate:predicate];由于“NSInvalidArgumentException”异常终止应用程序,原因:“不能使用非字符串的内容执行子字符串操作(lhs = iPhone rhs =( 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)‘
arrayTotalContacts有以下数据:
(
{
firstName = iPhone;
lastName = "";
},
{
firstName = Madhu;
lastName = "";
},
{
firstName = "Swa";
lastName = "";
},
{
firstName = TechV;
lastName = "";
},
{
firstName = Vedika;
lastName = Vt;
}
)发布于 2013-12-12 10:12:03
您可以在Core数据谓词中使用正则表达式:
[NSPredicate predicateWithFormat:@"NOT (firstName MATCHES %@)", @"^[A-Za-z].*"]其中,^[A-Za-z].*是以a或a开头的所有字符串的正则表达式。要使它也与来自外语的字母(例如“it”)一起工作,请使用Unicode属性名称:
[NSPredicate predicateWithFormat:@"NOT(firstName MATCHES %@)", @"^\\p{Letter}.*"]这里,^\\p{Letter}.*是以字母开头的所有字符串的正则表达式。
但是,如果这是针对表视图的,则最好使用获取的结果控制器及其sectionNameKeyPath参数。例如,见此处:
对于某些示例,如何根据初始字母对表视图进行分组。应该可以修改代码,将所有不以字母开头的名称分组到单独的组中。
发布于 2013-12-12 10:24:56
试试这个,
NSArray * arrContacts = [arrayTotalContacts valueForKeyPath:@"firstName"];希望它有帮助:)
https://stackoverflow.com/questions/20539909
复制相似问题