Following a suggestion from another member of the list, I'm now using 
NSNumberFormatter and NSDateFormatter (rather than NSScanner) to read some 
numbers and dates from strings retrieved from a web service. The service has 
its data formatted according to the pt_BR locale, which uses "." for grouping, 
"," for the decimal separator, and "dd/mm/yyyy" for dates. The problem is that, 
although the formatters are not ignoring the locale, they're still not parsing 
all strings correctly.

Here's the relevant code (in the app delegate of a test project):

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [self.window makeKeyAndVisible];

    [self readNumber: @"12"];

    [self readNumber: @"12.34"];
    [self readNumber: @"12,34"];

    [self readNumber: @"123456.78"];
    [self readNumber: @"123456,78"];

    [self readNumber: @"123,456.78"];
    [self readNumber: @"123.456,78"];

    [self readDate: @"23/04/2010"];

    return YES;
}

- (void) readNumber: (NSString*) str;
{
    NSLog(@" ");
    NSLog(@" str: %@", str);

    NSNumber* num = [self.numberFormatter numberFromString: str];
    NSLog(@" num: %@", num);
}

- (void) readDate: (NSString*) str;
{
    NSLog(@" ");
    NSLog(@" str: %@", str);

    NSDate* date = [self.dateFormatter dateFromString: str];
    NSLog(@"date: %@", date);
}

- (NSNumberFormatter*) numberFormatter;
{
    if (numberFormatter_ == nil)
    {
         numberFormatter_ = [[NSNumberFormatter alloc] init];
        [numberFormatter_ setLocale: self.localePtBr];
    }

    return numberFormatter_;
}

- (NSDateFormatter*) dateFormatter;
{
    if (dateFormatter_ == nil)
    {
         dateFormatter_ = [[NSDateFormatter alloc] init];
        [dateFormatter_ setLocale: self.localePtBr];
    }

    return dateFormatter_;
}

- (NSLocale*) localePtBr;
{
    if (localePtBr_ == nil)
    {
        localePtBr_ = [[NSLocale alloc] initWithLocaleIdentifier: @"pt_BR"];
    }

    return localePtBr_;
}

And here are the results:

[Session started at 2010-12-11 21:02:05 -0200.]
2010-12-11 21:02:07.905 FormatterTest[19914:207]  
2010-12-11 21:02:07.907 FormatterTest[19914:207]  str: 12
2010-12-11 21:02:07.908 FormatterTest[19914:207]  num: 12
2010-12-11 21:02:07.909 FormatterTest[19914:207]  
2010-12-11 21:02:07.909 FormatterTest[19914:207]  str: 12.34
2010-12-11 21:02:07.909 FormatterTest[19914:207]  num: (null)
2010-12-11 21:02:07.910 FormatterTest[19914:207]  
2010-12-11 21:02:07.910 FormatterTest[19914:207]  str: 12,34
2010-12-11 21:02:07.912 FormatterTest[19914:207]  num: 12.34
2010-12-11 21:02:07.912 FormatterTest[19914:207]  
2010-12-11 21:02:07.913 FormatterTest[19914:207]  str: 123456.78
2010-12-11 21:02:07.913 FormatterTest[19914:207]  num: (null)
2010-12-11 21:02:07.914 FormatterTest[19914:207]  
2010-12-11 21:02:07.914 FormatterTest[19914:207]  str: 123456,78
2010-12-11 21:02:07.915 FormatterTest[19914:207]  num: 123456.78
2010-12-11 21:02:07.916 FormatterTest[19914:207]  
2010-12-11 21:02:07.916 FormatterTest[19914:207]  str: 123,456.78
2010-12-11 21:02:07.917 FormatterTest[19914:207]  num: (null)
2010-12-11 21:02:07.917 FormatterTest[19914:207]  
2010-12-11 21:02:07.918 FormatterTest[19914:207]  str: 123.456,78
2010-12-11 21:02:07.918 FormatterTest[19914:207]  num: (null)
2010-12-11 21:02:07.919 FormatterTest[19914:207]  
2010-12-11 21:02:07.919 FormatterTest[19914:207]  str: 23/04/2010
2010-12-11 21:02:07.921 FormatterTest[19914:207] date: (null)
Terminating in response to SpringBoard's termination.

All results are correct, except for the last two. "123.456,78" should result in 
123456.78 and 23/04/2010 should result in the appropriate NSDate object for 
April 23, 2010.

The locale is NOT being ignored, since 12,34 and 123456,78 are parsed 
correctly. Yet, the group separator causes trouble and dates aren't being 
parsed correctly.

This is very frustrating. First, NSScanner ignores the group separator, though 
that's not documented anywhere that I could find (I learned about it from 
members of this list). Next, I've read the docs for the classes and the 
relevant sections on the Data Formatting Guide (Parsing and Creating Strings, 
for both Number and Date formatters), but very little concrete information is 
given on actually parsing a number or date in a given locale.

Is it not enough to set the formatter's locale? What am I missing?

Thanks in advance.
WT_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to