Take the 2-minute tour ×

I have a UITextField, which I am trying to use a NSNumberFormatter with a NSNumberFormatterDecimalStyle to convert the text field into an NSNumber to initialise an object.

However when I do this, I can't get any sensible results out of the conversion. For the purpose of this example I have replaced the UITextField with a string, but I still get strange results. I am sure I am doing something daft, but any help would be appreciated.

//NSString * boardNumberText = self.txtBoardNumbs.text;
NSString * boardNumberText = @"42";

NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSNumber * boardNumber = [formatter numberFromString:boardNumberText];

if ([boardNumber isEqual:nil]) {
    NSLog(@"Number was null");
NSLog(@"Number was not null");

[self.board setNumber:boardNumber];

NSLog([NSString stringWithFormat:@"%d", boardNumber]);
NSLog([NSString stringWithFormat:@"%d", self.board.number]);

The output that I get from the log file when I run this is:

2012-07-15 16:54:26.564 CoreDataDev[16123:fb03] Number was not null
2012-07-15 16:54:26.564 CoreDataDev[16123:fb03] 42
2012-07-15 16:54:26.565 CoreDataDev[16123:fb03] 135821152
2012-07-15 16:54:26.565 CoreDataDev[16123:fb03] 135820272
share|improve this question

2 Answers 2

up vote 0 down vote accepted

You can't just log NSNumber to the console using the integer format specifier, since NSNumber is a wrapper object. Also, NSLog takes a format string, so you don't need to use stringWithFormat:. Try this instead:

NSLog(@"%d", [boardNumber intValue]);
share|improve this answer
Ooops, Thanks everyone. Knew I was doing something dopey. –  Marryat Jul 15 '12 at 16:41


NSLog(@"%d", boardNumber);

is wrong: it prints the memory address of the boardNumber pointer (the NSNumber instance itself). Use



P. s.: that

NSLog([NSString stringWithFormat:...]);

is unnecessary and ugly; NSLog has built-in format string handling, use it!

share|improve this answer

Your Answer


