ios - How can I use an NSPredicate to select an exact phrase from an array? -


i using nspredicate filter nsdictionary list of states , countries.

all data stored in comma separated array: @"alabama", @"alaska", @"arizona", @"arkansas", @"california", @"colorado", etc

however, there 2 situations duplicates occurring. state "new mexico" contain word "mexico", , "arkansas" contains word "kansas.

here's basic code i'm using define predicate.

nspredicate *predicate = [nspredicate predicatewithformat:@"location contains[c] %@", @"kansas"]; 

how can make sure "kansas" doesn't return objects "arkansas"? using beginswith or doesn't work because list begins other state.

thanks!

let's see if understand correctly. have dictionary, there key "location" object string comma separated state names. sounds need regular expression match.

nsstring* expr = [nsstring stringwithformat:@"^%1$@.*|.*, %1$@.*", @"kansas"]   nspredicate* predicate = [nspredicate predicatewithformat:@"location matches[c] %@", expr]; 

now, here how behave different examples:

@{@"location": @"kansas, arkansas, alabama"} //pass @{@"location": @"alabama, kansas"} //pass @{@"location": @"arkansas, alabama"} //fail 

the regular expression ^%1$@.*|.*, %1$@.* tests if word provided either @ start of string or followed characters , ", ", puts in middle. %1$@ takes first param, can used multiple times instead of prividing same string several times.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -