數組篩選含有中文的方法:
1.
NSString *formatString = [NSStringstringWithFormat:@"(itemName CONTAINS[cd] '%1$@')", str];
[NSPredicatepredicateWithFormat:formatString]
2.
NSPredicate * p = [[NSPredicatepredicateWithFormat:@"(itemName CONTAINS[cd] $str) "]predicateWithSubstitutionVariables:@{@"str":str}];
- 第一種:str為'是會報錯 Unable to parse the format string "(itemName CONTAINS[cd] ''')
- 第二章:查詢成功:itemName CONTAINS[cd] "'"
補充:
%n$m@:代表輸出的是字符串,n代表是第幾個參數,設置m的值可以在輸出之前放置空格 %n$md:代表輸出的是整數,n代表是第幾個參數,設置m的值可以在輸出之前放置空格,也可以設為0m,在輸出之前放置m個0 %n$mf:代表輸出的是浮點數,n代表是第幾個參數,設置m的值可以控制小數位數,如m=2.2時,輸出格式為00.00
擴展
- 正則 輸入內容必須是數字和字母組合
NSString* number=@"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,14})$";
NSPredicate *numberPre = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",number];
BOOL result = [numberPreevaluateWithObject:self];
- 不包含在某個結果集里
[NSPredicatepredicateWithFormat:@" NOT (fid in %@) ",@"deaf"]
- 屬性和值參數化
NSPredicate *searchFor = [NSPredicatepredicateWithFormat:@"SELF = %@ AND %K = min(%@)",self, property, property];
- 復合篩選
[NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"age > 25"], [NSPredicate predicateWithFormat:@"firstName = %@", @"Quentin"]]];