NSPredicate的在查詢中的簡單使用,nspredicate查詢

來源:互聯網
上載者:User

NSPredicate的在查詢中的簡單使用,nspredicate查詢

上代碼吧,每塊代碼都可以直接跑,並附有注釋和運行結果

//直接使用 == 嚴格匹配NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"c", @"cd", @"cdd", @"cd", nil];NSString *match = @"cd";NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@", match];NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate];NSLog(@"result===%@",results);

列印結果:
result===(
cd,
cd
)

//like匹配,類似Sql中的用法NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"d", @"cd", @"cdd", @"dd",@"dcd",@"cdc", nil];NSString *match = @"c*";//以c開頭NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match];NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate];NSLog(@"result===%@",results);

列印結果:
result===(
c1,
cd,
cdd,
cdc
)

//忽略大小寫進行匹配NSArray *arrOriginal = [NSArray arrayWithObjects:@"Cdd",@"cDc",@"cdd",@"dcdd", nil];NSString *match = @"cd*";NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[c] %@", match];NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate];NSLog(@"result===%@",results);

列印結果:
result===(
Cdd,
cDc,
cdd
)

//利用正則表達是進行過濾NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"C3",@"d2",@"c2", nil];NSString *match = @"^c\\d?";NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate];NSLog(@"result===%@",results);

列印結果:
result===(
c1,
c2
)

//利用過濾器(數組)進行過濾NSArray *arrFilter = [NSArray arrayWithObjects:@"aa", @"bb", nil];NSArray *arrOriginal = [NSArray arrayWithObjects:@"dds",@"bb",@"c1",@"C3",@"bb",@"aa",@"c2", nil];NSPredicate *predicate  = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrFilter];//不包含符合過濾器的內容NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate];NSLog(@"result===%@",results);

列印結果:
result===(
dds,
c1,
C3,
c2
)


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.