Kir
Kir

Reputation: 8111

Objective C bug when converting NsNumber to nsString

NSNumber *abc = [[NSNumber alloc] initWithInt:123];
NSString *number = [NSString stringWithFormat:@"%d", abc];

The result of number is 31619! How to fix it?

Upvotes: 0

Views: 741

Answers (2)

rishi
rishi

Reputation: 11839

This is not an objective C bug, you are using wrong specifier-

Use -

NSString *number = [NSString stringWithFormat:@"%@", abc];

Upvotes: 4

Pfitz
Pfitz

Reputation: 7344

You have to return an intValue:

NSNumber *abc = [[NSNumber alloc] initWithInt:123];
NSString *number = [NSString stringWithFormat:@"%i", [abc intValue]];

Upvotes: 4

Related Questions