Question

I'm using NSLocale quite a lot for Numbers or Currency formatting. For example I use it this way:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
// Config the NSNumberFormatter ...
formatter.groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];

The compiler always gives me the warning: Multiple methods named 'objectForKey:' found

This gets really annoying in larger projects (20+ warnings of this type). The only way I found to get rid of this warning is doing a type cast to NSDictionary:

formatter.groupingSeparator = [(NSDictionary *)[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];

This works but I'm not sure if this will lead to problems as [NSLocale currentLocale] seems not directly to be an NSDictionary ([[NSLocale currentLocale] class] returns __NSCFLocale).

Is there any better solution to this?

Était-ce utile?

La solution

CMD+Click to your objectForKey statement. Xcode will find the method at NSDictionary.h.

Now change your code like

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
...
NSLocale *currentLocale = [NSLocale currentLocale];
formatter.groupingSeparator = [currentLocale objectForKey:NSLocaleGroupingSeparator];

and CMD+Click again to this objectForKey statement. Xcode will go to correct place, NSLocale.h.

Or, as you suggested, you can just force-cast NSLocale like

formatter.groupingSeparator = [(NSLocale *)[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top