iphone - find special keys in UserDefaults by "substringToIndex" -
i'm grouping userdefault keys specific prefixes.
e.g.
[nsuserdefaults standarduserdefaults] setinteger: 1 forkey: @"prefix1_sometext_key"] [nsuserdefaults standarduserdefaults] setinteger: 2 forkey: @"prefix2_sometext_key"] [nsuserdefaults standarduserdefaults] setinteger: 3 forkey: @"prefix4_sometext_key"] //..... now, i'd find keys, start e.g. "prefix", , load them array. there way doing (programmatically)?
you use underlying nsdictionary find suitable keys:
nsstring *myprefix = @"prefix"; nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; nsdictionary *dict = [defaults dictionaryrepresentation]; nsmutablearray *keyswithprefix = [nsmutablearray array] (nsstring *key in dict.keyenumerator) { if ([key hasprefix: myprefix]) { [keyswithprefix addobject: key]; } } // keyswithprefix contains matching keys update debugging reasons add log see keys being dropped:
for (nsstring *key in dict.keyenumerator) { if ([key hasprefix: myprefix]) { [keyswithprefix addobject: key]; } else { nslog(@"dropping key %@", key); } }
Comments
Post a Comment