如何通过firstName过滤NSMutableArray与非字母字符 [英] How can i filter NSMutableArray by firstName with non-alphabetical characters
问题描述
我想过滤数组,其中名称以非字母字符开头。我想在表视图中的不同部分下显示firstName以非字母字符开头的联系人。我尝试下面的代码,但它崩溃,请找到原因下面:
NSPredicate * predicate = [NSPredicate predicateWithFormat:@NOT firstName BEGINSWITH [c]%@),arrIndex]; //其中arrIndex是字母字符数组。
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,
arrayTotalContacts具有以下数据:
(
{
firstName = iPhone;
lastName =;
}
{
firstName = Madhu;
lastName =;
},
{
firstName =Swa;
lastName = ;
},
{
firstName = TechV;
lastName =;
},
{
firstName = Vedika;
lastName = Vt;
}
)
您可以使用带有Core Data谓词的正则表达式:
[NSPredicate predicateWithFormat:@NOT MATCHES%@),@^ [A-Za-z]。*]
^ [A-Za-z]。*
是以AZ或az开头的所有字符串的正则表达式。
要使其与外语的字母(例如Ä)一起使用,请使用
Unicode属性名称:
[NSPredicate predicateWithFormat:@NOT(firstName MATCHES%@),@^ \\p {Letter}。*]
这里 ^ \\p {Letter}。*
是一个正则表达式,字母。
但如果这是一个表视图,你最好使用一个获取结果控制器
和它的 sectionNameKeyPath
参数。请参见此处:
一些示例如何根据首字母分组表视图。应该是
可能修改代码以将不以字母开头的所有名称分组到
单独的组。
I want to filter the Array, where name starts with non-alphabetical characters. I want to display the contacts where firstName starts with non-alphabetical characters under different section in table view. I tried below code, but it crashing , please find reason below:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT(firstName BEGINSWITH[c] %@)",arrIndex]; //where arrIndex is the array of alphabetical characeters.
NSArray *arrContacts = [arrayTotalContacts filteredArrayUsingPredicate:predicate];
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do a substring operation with something that isn't a string (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 has the below data:
(
{
firstName = iPhone;
lastName = "";
},
{
firstName = Madhu;
lastName = "";
},
{
firstName = "Swa";
lastName = "";
},
{
firstName = TechV;
lastName = "";
},
{
firstName = Vedika;
lastName = Vt;
}
)
You can use regular expression with a Core Data predicate:
[NSPredicate predicateWithFormat:@"NOT (firstName MATCHES %@)", @"^[A-Za-z].*"]
where ^[A-Za-z].*
is a regex for all strings that do start with A-Z or a-z.
To make it work with letters from foreign languages as well (e.g. "Ä"), use
the Unicode property name:
[NSPredicate predicateWithFormat:@"NOT(firstName MATCHES %@)", @"^\\p{Letter}.*"]
Here ^\\p{Letter}.*
is a regex for all strings that start with a letter.
But if this is for a table view, you might better use a fetched results controller
and its sectionNameKeyPath
parameter. See for example here:
for some examples how to group a table view according to the initial letter. It should be possible to modify the code to group all names that do not start with a letter into a separate group.
这篇关于如何通过firstName过滤NSMutableArray与非字母字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!