eMich
eMich

Reputation: 1736

NSString to NSNumber : what am I doing wrong here?

I am executing this code:

NSLog(@"Converting line number %@ to int...",currentValue);

NSNumber * currentNumber = [NSNumber numberWithInt:[currentValue integerValue]];

NSLog(@"Converted integer %d",currentNumber);

Which outputs this:

Converting line number 211 to int...
Converted integer 62549488

What am I doing wrong here ? In this case, currentValue is a NSMutableString with value of 211.

Upvotes: 2

Views: 2131

Answers (3)

EXC_BAD_ACCESS
EXC_BAD_ACCESS

Reputation: 2707

NSNumber is inherited NSObject class.Its not an Integer.So you can use "%@" instead of %d.

You change your NSLog line as follows,

NSLog(@"Converted integer %@",currentNumber);

Upvotes: 2

Abizern
Abizern

Reputation: 150565

Although your question has already been answered there is something else wrong with your code.

There are differences between Cocoa methods with int and integer in their names.

The ones with int such as numberWithInt or intValue deal with int s.

The ones with integer such as numberWithInteger or integerValue deal with NSInteger s.

This might be a source of errors and inconsistency when programming 32/64-bit programs.

Just something to be aware of and consistent with.

Upvotes: 5

sidyll
sidyll

Reputation: 59277

"currentNumber" is and object, which you're trying to print with %d.

NSLog(@"Converted integer %d", [currentNumber intValue]);

Upvotes: 11

Related Questions