开发者

NSString to NSNumber : what am I doing wrong here?

I am executing this code:

NSLog(开发者_Go百科@"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.


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

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


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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜