I have an Objective-C method that formats a double to a currency NSString:
- (NSString*) formatCurrencyValue:(double)value
{
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setCurrencySymbol:@"$"];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber *c = [NSNumber numberWithFloat:value];
NSString* stringValue = [numberFormatter stringFromNumber:c];
NSLog(@"currencyFormatter=%@",stringValue);
return stringValue;
}
The NSLog statement correctly prints the formatted double. However, when I call this method and assign it another NSString object, its value is empty.
For example:
NSString* key = [self formatCurrencyValue:menuItem.Price];
NSLog(@"Key value=", key);
The output from the NSLog statement there is:
Key value=
What’s going on???
In the line
NSLog(@"Key value=", key);, you’re missing the%@placeholder that tells Objective-C where to print the argument.It should be
NSLog(@"Key value=%@", key);.