views:

13

answers:

1

I've been having some trouble getting NSNumberFormatter to work properly.

I'm compiling in iOS4 with the Three20 Framework.

All of the NSNumberFormatter selectors require something called NS_AVAILABLE as a second parameter on the selector:

[numberFormatter setCurrencyGroupingSeparator:(NSString *)string __AVAILABILITY_INTERNAL__IPHONE_2:(int)_0];

I'm not sure what I'm supposed to do with the second parameter. I've tried:

[numberFormatter setCurrencyGroupingSeparator:@"," __AVAILABILITY_INTERNAL__IPHONE_2:2]; // Warning: NSNumberFormatter' may not respond to '-setCurrencyGroupingSeparator:__AVAILABILITY_INTERNAL__IPHONE_2:
[numberFormatter setCurrencyGroupingSeparator:@"," __AVAILABILITY_INTERNAL__IPHONE_2:2_0]; //Error: invalid suffix "_0" on integer constant

and several more iterations of that.

If I don't specify the __AVAILABLE_INTERNAL__IPHONE_2 it doesn't throw a warning and compiles fine, but the selector text is black, not dark purple in Xcode as if it's not recognized.

Here's the full code snippet:

NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setCurrencyGroupingSeparator:@","];
[numberFormatter setUsesGroupingSeparator:YES];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
numberString = [NSString stringWithFormat: @"$%@",[numberFormatter stringFromNumber:myNsNumber]]; // myNsNumber is an NSNumber with a value of 10000000
NSLog(@"numberString: %@",numberString); // numberString: (null) | instead of numberString: $10,000,000

I've Googled _AVAILABILITY_INTERNAL__IPHONE_2 and NS_AVAILABLE with no luck.

Searching developer.apple.com only gives me the iOS 4.0 API Diffs.

Is this an issue with Three20? Does anyone know whats going on here or what NS_AVAILABLE is all about?

A: 

Forget about just about everything you have in this post so far. The availability stuff lets you know what SDK the methods are included in.

This is working NSNumberFormatter code.

NSNumber *number = [UINumber numberWithInt:42];
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
[nf setFormatterBehavior:NSNumberFormatterBehavior10_4];
[nf setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *string = [nf stringFromNumber:number];
[nf release];

If you have a null string, then it is either because your NSNumber is null or NaN, or your format is screwed up.

coneybeare