Generally this is quite a lot, for example, you read a array1 from a file, and then you want to filter out an element in the program that conforms to the content in Array1. Array2.
1) Example one, a loop
Nsarray *arrayfilter = [Nsarray arraywithobjects:@ "pict", @ "Blackrain", @ "IP", nil];
Nsarray *arraycontents = [Nsarray arraywithobjects:@ "I am a picture." @ "I'm a guy" @ "I am Gagaga" @ "ipad" @ "iphone", NIL];
I want to filter arraycontents, just loop arrayfilter.
int i = 0, count = [Arrayfilter count];
for (i = 0; i < count; i + +)
{
NSString *arrayitem = (NSString *) [Arrayfilter objectatindex:i];
Nspredicate *filterpredicate = [[Nspredicate predicatewithformat:@ "Self CONTAINS%@", Arrayitem];
NSLog (@ "Filtered array with filter%@,%@", Arrayitem, [arraycontents filteredarrayusingpredicate:filterpredicate]);
}
Of course the above code arraycontent best use mutable, so you can directly filter, Nsarray is not modifiable.
2) Example Two, no loop required
Nsarray *arrayfilter = [Nsarray arraywithobjects:@ "ABC1", @ "ABC2", nil];
Nsarray *arraycontent = [Nsarray arraywithobjects:@ "A1", @ "ABC1", @ "ABC4", @ "ABC2", nil];
Nspredicate *thepredicate = [nspredicate predicatewithformat:@ ' not ' (self in%@) ", Arrayfilter];
[Arraycontent Filterusingpredicate:thepredicate];
So arraycontent filter out is not to include all the item in the Arrayfilter.
3) Generate file path under File collection list
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
NSString *defaultpath = [[NSBundle mainbundle] resourcepath];
Nserror *error;
Nsarray *directorycontents = [FileManager contentsofdirectoryatpath:defaultpath error:&error]
4) Usage of match
1. Simple comparison
NSString *match = @ "Imagexyz-999.png";
Nspredicate *predicate = [Nspredicate predicatewithformat:@ "self = =%@", match];
Nsarray *results = [directorycontents filteredarrayusingpredicate:predicate];
2. Usage of the like in match (similar to the usage in SQL)
NSString *match = @ "Imagexyz*.png";
Nspredicate *predicate = [Nspredicate predicatewithformat:@ "Self like%@", match];
Nsarray *results = [directorycontents filteredarrayusingpredicate:predicate];
3. Case comparison
[c] means ignoring the case, [d] means ignoring the accent and can be used together as follows:
NSString *match = @ "Imagexyz*.png";
Nspredicate *predicate = [Nspredicate predicatewithformat:@ "self like[cd]%@", match];
Nsarray *results = [directorycontents filteredarrayusingpredicate:predicate];
4. Using the regular
NSString *match = @ "Imagexyz-\\d{3}\\.png"; Imagexyz-123.png
Nspredicate *predicate = [Nspredicate predicatewithformat:@ "self matches%@", match];
Nsarray *results = [directorycontents filteredarrayusingpredicate:predicate];
Summarize:
1) When using an operator of an aggregate class, it is not necessary to loop the
2) when using an operator of a single comparison class, you can take a loop to get it done
PS, example one tries to use [@ "Self CONTAINS%@", Arrayfilter] to filter will hang, because CONTAINS when the string comparison operator, not the set operator.
The usage of nspredicate http://www.cnblogs.com/MarsGG/articles/1949239.html
Nspredicate usage, array de-weight, comparison ...