So how do you find a certain NSDictionary from NSArray containing a requested key? Two easy ways here:
This is what I usually did. Simple code, easy to understand, easy to debug, self-documenting, portable. Just a bit long:
- (NSInteger)findValue:(NSString *)value withKey:(NSString *)keyThis is what I'm using now. Non-portable iOS-only code, but it's shorter:
{
if (!self.dictList)
return -1;
NSInteger count = 0;
for (NSDictionary *dict in self.dictList)
{
if ([[dict objectForKey:key] isEqualToString:value])
return count;
else
count++;
}
return -1;
}
- (NSInteger)findValue:(NSString *)value withKey:(NSString *)keyThe annoying first two lines are needed, since this code...
{
if (!self.dictList)
return -1;
NSArray *a = [self.dictList valueForKey:key];
NSInteger index = [a indexOfObject:value];
return index == NSNotFound ? -1: index;
}
NSArray *a = nil;...assings index == 0, which looks like a valid value. Work in progress. Also got to refactor rest of my code to treat NSIntegerMax as "not found" in stead of current "-1" value. More work in progress.
NSInteger index = [a indexOfObject:@"test"];
thank you so much !!
ReplyDelete