objective c - NSPredicate to match string with an array of prefixes -
i'm unsure how write nspredicate
achieve following. have array of prefixes, , want know if of them (plus underscore) prefix of given string. don't need know matched, yes/no if matched @ all.
i can't seem work out, @ moment have this
#import <foundation/foundation.h> int main(int argc, char *argv[]) { @autoreleasepool { nsarray *bins = @[@"aaa", @"bbb", @"ccc"]; nsstring* item = @"aaa_blah"; nspredicate *pred = [nspredicate predicatewithformat:@"%@ beginswith self", item]; nslog(@"%@", [[bins filteredarrayusingpredicate:pred] count] ? @"yes" : @"no"); } }
the way think of doing filtering array - firstly there better approach?
and secondly, want return true if prefix followed underscore so
@[@"aaa", @"bbb", @"ccc"]; @"aaa_blah"; // yes @"aaablah"; // no @"bbbblah"; // no
i'm not sure how that?
+(void)checkifexists:(nsarray *)prefixes inmyobjects:(nsarray *)myobjects withdivider:(nsstring *)divider { divider = @"_"; prefixes = @[@"aaa",@"bbb",@"ccc"]; myobjects = @[@"aaa_sd",@"dsf_ds",@"aaa_sss",@"aaabbb"]; nsmutablearray * resultsofpredicate = [nsmutablearray new]; (nsstring * pre in prefixes) { nsstring * iamlookingfor = [nsstring stringwithformat:@"%@%@", pre, divider]; nspredicate *prefixpredicate = [nspredicate predicatewithformat:@"self beginswith[c] %@", iamlookingfor]; nsarray * resultofsearch = [myobjects copy]; resultofsearch = [resultofsearch filteredarrayusingpredicate:prefixpredicate]; nslog(@"ros %@",resultofsearch); [resultsofpredicate addobject:@([resultofsearch count])]; } (int = 0; i<[resultsofpredicate count]; i++) { nslog(@"prefix %@ isappeared:%d",[prefixes objectatindex:i], [[resultsofpredicate objectatindex:i] boolvalue]); } }
i hope help.
Comments
Post a Comment