I'm developing an iPhone application.
I use a NSDictionary to store city's names as key, and population as value. I want to search the keys using lowercase.
I've using this:
NSDictionary *dict;
[dict objectForKey:[[city stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] lowercaseString]];
But, it doesn't work.
I know, I can do a for, convert keys to lowercase and compare with city.
Is there any other way to do that? Maybe, with a NSDictionary method.
UPDATE
The NSDictionary is loaded from a property list.
Thank you.
I use this method in an NSDictionary category.
@implementation NSDictionary (MyCategory)
- (NSDictionary *)dictionaryWithLowercaseKeys {
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:0];
NSString *key;
for (key in self) {
[result setObject:[self objectForKey:key] forKey:[key lowercaseString]];
}
return result;
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With