Objective-C Language
NSPredicate
サーチ…
構文
CONTAINS演算子:サブセットが一致するオブジェクトをフィルタリングできます。
NSPredicate *filterByName = [NSPredicate predicateWithFormat:@"self.title CONTAINS[cd] %@",@"Tom"];
LIKE:単純比較フィルタ。
NSPredicate *filterByNameCIS = [NSPredicate predicateWithFormat:@"self.title LIKE[cd] %@",@"Tom and Jerry"];
=演算子:フィルタ値が一致するすべてのオブジェクトを返します。
NSPredicate *filterByNameCS = [NSPredicate predicateWithFormat:@"self.title = %@",@"Tom and Jerry"];
IN演算子:特定のフィルタセットでオブジェクトをフィルタリングできます。
NSPredicate *filterByIds = [NSPredicate predicateWithFormat:@"self.id IN %@",@[@"7CDF6D22-8D36-49C2-84FE-E31EECCECB79", @"7CDF6D22-8D36-49C2-84FE-E31EECCECB76"]];
NOT IN演算子:特定の集合を持つ逆オブジェクトを見つけることができます。
NSPredicate *filterByNotInIds = [NSPredicate predicateWithFormat:@"NOT (self.id IN %@)",@[@"7CDF6D22-8D36-49C2-84FE-E31EECCECB79", @"7CDF6D22-8D36-49C2-84FE-E31EECCECB76"]];
備考
詳細についてはAppleのドキュメントのNSPredicateを読んでください
名前でフィルタリングする
NSArray *array = @[
@{
@"id": @"7CDF6D22-8D36-49C2-84FE-E31EECCECB71",
@"title": @"Jackie Chan Strike Movie",
@"url": @"http://abc.com/playback.m3u8",
@"thumbnailURL": @"http://abc.com/thumbnail.png",
@"isMovie" : @1
},
@{
@"id": @"7CDF6D22-8D36-49C2-84FE-E31EECCECB72",
@"title": @"Sherlock homes",
@"url": @"http://abc.com/playback.m3u8",
@"thumbnailURL": @"http://abc.com/thumbnail.png",
@"isMovie" : @0
},
@{
@"id": @"7CDF6D22-8D36-49C2-84FE-E31EECCECB73",
@"title": @"Titanic",
@"url": @"http://abc.com/playback.m3u8",
@"thumbnailURL": @"http://abc.com/thumbnail.png",
@"isMovie" : @1
},
@{
@"id": @"7CDF6D22-8D36-49C2-84FE-E31EECCECB74",
@"title": @"Star Wars",
@"url": @"http://abc.com/playback.m3u8",
@"thumbnailURL": @"http://abc.com/thumbnail.png",
@"isMovie" : @1
},
@{
@"id": @"7CDF6D22-8D36-49C2-84FE-E31EECCECB75",
@"title": @"Pokemon",
@"url": @"http://abc.com/playback.m3u8",
@"thumbnailURL": @"http://abc.com/thumbnail.png",
@"isMovie" : @0
},
@{
@"id": @"7CDF6D22-8D36-49C2-84FE-E31EECCECB76",
@"title": @"Avatar",
@"url": @"http://abc.com/playback.m3u8",
@"thumbnailURL": @"http://abc.com/thumbnail.png",
@"isMovie" : @1
},
@{
@"id": @"7CDF6D22-8D36-49C2-84FE-E31EECCECB77",
@"title": @"Popey",
@"url": @"http://abc.com/playback.m3u8",
@"thumbnailURL": @"http://abc.com/thumbnail.png",
@"isMovie" : @1
},
@{
@"id": @"7CDF6D22-8D36-49C2-84FE-E31EECCECB78",
@"title": @"Tom and Jerry",
@"url": @"http://abc.com/playback.m3u8",
@"thumbnailURL": @"http://abc.com/thumbnail.png",
@"isMovie" : @1
},
@{
@"id": @"7CDF6D22-8D36-49C2-84FE-E31EECCECB79",
@"title": @"The wolf",
@"url": @"http://abc.com/playback.m3u8",
@"thumbnailURL": @"http://abc.com/thumbnail.png",
@"isMovie" : @1
}
];
// *** Case Insensitive comparision with excate title match ***
NSPredicate *filterByNameCIS = [NSPredicate predicateWithFormat:@"self.title LIKE[cd] %@",@"Tom and Jerry"];
NSLog(@"Filter By Name(CIS) : %@",[array filteredArrayUsingPredicate:filterByNameCIS]);
指定されたID以外の映画を探す
// *** Find movies except given ids ***
NSPredicate *filterByNotInIds = [NSPredicate predicateWithFormat:@"NOT (self.id IN %@)",@[@"7CDF6D22-8D36-49C2-84FE-E31EECCECB79", @"7CDF6D22-8D36-49C2-84FE-E31EECCECB76"]];
NSLog(@"Filter movies except given Ids : %@",[array filteredArrayUsingPredicate:filterByNotInIds]);
タイプムービーであるすべてのオブジェクトを検索する
// *** Find all the objects which is of type movie, Both the syntax are valid ***
NSPredicate *filterByMovieType = [NSPredicate predicateWithFormat:@"self.isMovie = %@",@1];
// OR
//NSPredicate *filterByMovieType = [NSPredicate predicateWithFormat:@"self.isMovie = %@",[NSNumber numberWithBool:YES]];
NSLog(@"Filter By Movie Type : %@",[array filteredArrayUsingPredicate:filterByMovieType]);
配列のDistinctオブジェクトIDを検索する
// *** Find Distinct object ids of array ***
NSLog(@"Distinct id : %@",[array valueForKeyPath:@"@distinctUnionOfObjects.id"]);
特定のIDを持つ映画を探す
// *** Find movies with specific ids ***
NSPredicate *filterByIds = [NSPredicate predicateWithFormat:@"self.id IN %@",@[@"7CDF6D22-8D36-49C2-84FE-E31EECCECB79", @"7CDF6D22-8D36-49C2-84FE-E31EECCECB76"]];
NSLog(@"Filter By Ids : %@",[array filteredArrayUsingPredicate:filterByIds]);
正確なタイトル一致と大文字と小文字を区別しない比較
// *** Case Insensitive comparison with exact title match ***
NSPredicate *filterByNameCIS = [NSPredicate predicateWithFormat:@"self.title LIKE[cd] %@",@"Tom and Jerry"];
NSLog(@"Filter By Name(CIS) : %@",[array filteredArrayUsingPredicate:filterByNameCIS]);
大文字と小文字が区別さ
// *** Case sensitive with exact title match ***
NSPredicate *filterByNameCS = [NSPredicate predicateWithFormat:@"self.title = %@",@"Tom and Jerry"];
NSLog(@"Filter By Name(CS) : %@",[array filteredArrayUsingPredicate:filterByNameCS]);
大文字と小文字を区別しないサブセットとの比較
// *** Case Insensitive comparison with matching subset ***
NSPredicate *filterByName = [NSPredicate predicateWithFormat:@"self.title CONTAINS[cd] %@",@"Tom"];
NSLog(@"Filter By Containing Name : %@",[array filteredArrayUsingPredicate:filterByName]);