简体   繁体   中英

Check if an object exist in a NSMutableArray of NSDictionary

i have an NSMutableArray that contains some NsDictionary with NSstring for differents Keys. Somethings like:

NSDictionary *ExempleDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[Date objectAtIndex:sender.tag],@"DATE",[Time objectAtIndex:sender.tag],@"TIME", nil];

I'd like to check if a particular object with DATE=xxx && TIME=xxx exist in the Array.

Any idea?!

You can do the search with NSPredicate :

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.DATE=%@ AND SELF.TIME=%@", dateVal, timeVal];
NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicate];

filteredArray contains an NSArray of all NSDictionary objects matching the specified date and time condition. You can retrieve the matching objects by iterating the filtered array.

You can use NSArray's containsObject: (if you have a reference to the dictionary laying around) or indexOfObjectIdenticalTo: (if you want to check if an element identical to the one you are passing -but not necessarily the same object- is in the array). Both are explained in the docs:

containsObject

indexOfObjectIdenticalTo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM