objective c - NSPredicate to check if attribute of object exits, if it does, get it -
i'm not sure of possible, if is, surely help.
i have nsarray of nsdictionaries.
each dictionary has keys (obviously).
dict{ title: wbccount cat: lab } dict{ title: hbm cat: lab sex: male } dict{ title: hbf cat: lab sex: female } dict{ title: pc_count cat: cbc sex: female } i wanted filter array dictionaries have cat = 'lab' , if sex key present in dictionary object, 1 male.
in short, cannot put
predicatewithformate:%@" cat = lab , ( if sex key present, sex = male"; this me array of wbc, hbm.
i don't know if possible, condition within predicate, life saver if how objects sent via web api.
any other way of achiving goal if not great.
while in subject of core data, should simple: want have attribute of entity able store either nsdate or nsnumber or nsstring. there easy way out?
the trick here in nsdictionary, non-present key returns nil:
nsarray *dictionaries = @[ @{ @"title": @"t1", @"cat": @"lab", @"sex": @"male" }, @{ @"title": @"t2", @"cat": @"c2" }, @{ @"title": @"t3", @"cat": @"lab", @"sex": @"female" }, @{ @"title": @"t4", @"cat": @"lab" } ]; nspredicate *pred = [nspredicate predicatewithformat:@"(sex == nil or sex = 'male') , cat = 'lab'" ]; nsarray *result = [dictionaries filteredarrayusingpredicate:pred];
Comments
Post a Comment