谓词NSPredicate的使用
生活随笔
收集整理的這篇文章主要介紹了
谓词NSPredicate的使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? ? 謂詞是用來為數(shù)據(jù)添加過濾條件,從而更加精確的找到找到所描述條件的數(shù)據(jù)。蘋果為我們封裝好了NSPredicate類,我們可以很方便的得到我們需要的過濾條件。
? ? ? 謂詞的簡單語法總結(jié):
? ? ? 比較運(yùn)算:
| ?> | 大于 |
| < | 小于 |
| ?>= | 大于等于 |
| ?<= | 小于等于 |
| == | 等于 |
| != | 不等于 |
| between | 左邊的表達(dá)式等于右邊的表達(dá)式的值或者介于它們之間。 右邊是一個(gè)有兩個(gè)指定上限和下限的數(shù)值的數(shù)列 (指定順序的數(shù)列) |
?
? ? ? ?
? ? ? ? ??
?
?
?
?
?
? ?
?
?
?
?關(guān)系運(yùn)算符:
| ANY,SOME | 指定下列表達(dá)式中的任意元素 |
| ALL | 指定下列表達(dá)式中的所有元素 |
| NONE | 指定下列表達(dá)式中沒有的元素。 |
| IN | 左邊的表達(dá)必須出現(xiàn)在右邊指定的集合中。 比如,name IN { '昊天', '望舒', '墨雪' }。 |
| BEGINSWITH | 以xx開頭 |
| ENDSWITH | 以xx結(jié)尾 |
| CONTAINS | 其中包含xx |
| like | 匹配任意多個(gè)字符 |
?
?
?
?
?
?
?
?
?
?
?
?
1.查找數(shù)組中
NSArray *humans = [NSArray arrayWithObjects:[Human personWithName:@"cat" andAge:0],[Human personWithName:@"dog" andAge:1],[Human personWithName:@"bird" andAge:2],[Human personWithName:@"tiger" andAge:3],[Human personWithName:@"lion" andAge:4],[Human personWithName:@"monkey" andAge:5],[Human personWithName:@"cow" andAge:6],[Human personWithName:@"dargen" andAge:7],[Human personWithName:@"bear" andAge:8], nil];//使用謂詞條件運(yùn)算符NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age > 5"];NSArray *array1 = [humans filteredArrayUsingPredicate:predicate];//將謂詞添加到數(shù)組中NSLog(@"%@",array1); //使用謂詞中的in進(jìn)行過濾predicate = [NSPredicate predicateWithFormat:@"name IN {'bird','lion'}"];NSArray *array2 = [humans filteredArrayUsingPredicate:predicate];NSLog(@"%@",array2); //使用betweenpredicate = [NSPredicate predicateWithFormat:@"age between {2,5}"];NSArray *array3 = [humans filteredArrayUsingPredicate:predicate];NSLog(@"%@",array3); //使用likepredicate = [NSPredicate predicateWithFormat:@"name like '?o*'"];//?表示一個(gè)字符串,且第二個(gè)字符串為oNSArray *array5 = [humans filteredArrayUsingPredicate:predicate];NSLog(@"%@",array5);?
//使用BEGINSWITHpredicate = [NSPredicate predicateWithFormat:@"name beginswith 'd'"];NSArray *array4 = [humans filteredArrayUsingPredicate:predicate];NSLog(@"%@",array4);轉(zhuǎn)載于:https://www.cnblogs.com/moxuexiaotong/p/4972511.html
總結(jié)
以上是生活随笔為你收集整理的谓词NSPredicate的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WIN7 中配置局域网
- 下一篇: Hadoop学习笔记—11.MapRed