NSPredicate謂詞-數(shù)組過濾

NSPredicate謂詞-數(shù)組過濾

NSPredicate中主要的幾種運算方式

1、比較運算符: > 先蒋、< 骇钦、== 、 >= 鞭达、<= 司忱、 !=
數(shù)值
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"score >= 80"];
字符串
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"certType == \'NI\'"];
certType:是自定義類的屬性或者是字典的key,字符串NI要用單引號
2畴蹭、邏輯運算符:and坦仍、or、not
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type >= 3 and score >= 80"];
3叨襟、范圍運算符:in繁扎、between
4、字符串本身:self
5、字符串相關(guān):beginswith梳玫、endswith爹梁、contains
NSString *str = @"good";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains[cd] %@",str];

注:[c]不區(qū)分大小寫 , [d]不區(qū)分發(fā)音符號即沒有重音符號 , [cd]既不區(qū)分大小寫,也不區(qū)分發(fā)音符號提澎。

6姚垃、通配符:like
NSString *str = @"good*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self like[cd] %@",str];
7、正則表達式:matches

例:NSString regex = @"good";

//數(shù)字盼忌、字母积糯、-組成,5-10位
NSString * regex = @"^[0-9A-Za-z-]{5,10}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL result = [pred evaluateWithObject:self.passenger.rPostCode];
正確為yes

對對象集合的篩選

NSString *name = @"name";
NSString *str = @"005";
NSPredicate *predicate0 = [NSPredicate predicateWithFormat:@"%@ == %@",name,str];
//po predicate1.predicateFormat:"name" == "005"谦纱,零個結(jié)果看成,語法不對
NSArray *predicateArray0 = [array filteredArrayUsingPredicate:predicate0];

NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"self.%@ == %@",name,str];
//po predicate1.predicateFormat:SELF."name" == "005",1個結(jié)果跨嘉,語法正確
NSArray *predicateArray1 = [array filteredArrayUsingPredicate:predicate1];

NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"name == %@",str];
//po predicate2.predicateFormat: name == "005"川慌,有一個結(jié)果,語法正確
NSArray *predicateArray2 = [array filteredArrayUsingPredicate:predicate2];

NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"name == \'005\'"];
//po predicate3.predicateFormat: name == "005"祠乃,有一個結(jié)果梦重,語法z'q
NSArray *predicateArray3 = [array filteredArrayUsingPredicate:predicate3];

附上源碼:

//
//  GGPredicateViewController.m
//  testDemo
//
//  Created by lignpeng on 2017/7/24.
//  Copyright ? 2017年 genpeng. All rights reserved.
//

#import "GGPredicateViewController.h"

@interface User : NSObject

@property(nonatomic, strong) NSString *name;
@property(nonatomic, assign) NSInteger type;
@property(nonatomic, assign) NSInteger score;

@end

@implementation User

+ (instancetype)user:(NSString *)name type:(NSInteger )type {
    User *user = [User new];
    user.name = name;
    user.type = type;
    user.score = arc4random()%100;
    return user;
}

@end

@interface GGPredicateViewController ()

@end

@implementation GGPredicateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)initView {
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat margin = 32;
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(margin, margin * 3, CGRectGetWidth([UIScreen mainScreen].bounds) - margin * 2, 42)];
    [button addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"action" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor blueColor];
    button.layer.cornerRadius = 5;
    button.clipsToBounds = YES;
    [self.view addSubview:button];
}

- (void)action {
    [self objects];
    [self strings];
}

- (void)objects{
    NSMutableArray *array = [NSMutableArray array];
    NSUInteger index = 6;
    while (index > 0) {
        [array addObject:[User user:[NSString stringWithFormat:@"00%lu",(unsigned long)index] type:index]];
        index--;
    }
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type >= 3 or score >= 80"];
    //po predicate.predicateFormat: type >= 3 OR score >= 80
    NSArray *predicateArray = [array filteredArrayUsingPredicate:predicate];
    NSString *name = @"name";
    NSString *str = @"005";
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"%@ == %@",name,str];
    //po predicate1.predicateFormat:"name" == "005",零個結(jié)果
    NSArray *predicateArray1 = [array filteredArrayUsingPredicate:predicate1];
    
    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"name == %@",str];
    //po predicate2.predicateFormat: name == "005"亮瓷,有一個結(jié)果
    NSArray *predicateArray2 = [array filteredArrayUsingPredicate:predicate2];
}

- (void)strings {
    NSArray *array = @[@"willback",@"backhome",@"goodmonring",@"gooDbyd"];
    NSString *str = @"good";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains[c] %@",str];
    //有兩個結(jié)果
    NSArray *predicateArray = [array filteredArrayUsingPredicate:predicate];
    
    str = @"gooDbyd";
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"self == %@",str];
    //po predicate1 SELF == "gooDbyd"忍饰,有一個結(jié)果
    NSArray *predicateArray1 = [array filteredArrayUsingPredicate:predicate1];
}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市寺庄,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌力崇,老刑警劉巖斗塘,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異亮靴,居然都是意外死亡馍盟,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門茧吊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來贞岭,“玉大人,你說我怎么就攤上這事搓侄∶榻埃” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵讶踪,是天一觀的道長芯侥。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么柱查? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任廓俭,我火速辦了婚禮,結(jié)果婚禮上唉工,老公的妹妹穿的比我還像新娘研乒。我一直安慰自己,他們只是感情好淋硝,可當(dāng)我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布雹熬。 她就那樣靜靜地躺著,像睡著了一般奖地。 火紅的嫁衣襯著肌膚如雪橄唬。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天参歹,我揣著相機與錄音仰楚,去河邊找鬼。 笑死犬庇,一個胖子當(dāng)著我的面吹牛僧界,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播臭挽,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼捂襟,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了欢峰?” 一聲冷哼從身側(cè)響起葬荷,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎纽帖,沒想到半個月后宠漩,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡懊直,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年扒吁,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片室囊。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡雕崩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出融撞,到底是詐尸還是另有隱情盼铁,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布懦铺,位于F島的核電站捉貌,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜趁窃,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一牧挣、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧醒陆,春花似錦瀑构、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至澡刹,卻和暖如春呻征,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背罢浇。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工陆赋, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人嚷闭。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓攒岛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親胞锰。 傳聞我的和親對象是個殘疾皇子灾锯,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容