#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSNumber *str = [NSNumber numberWithChar:'x'];
NSLog(@"%@",str);
}
return 0;
}
This prints the number 120, instead of the character ‘x’. Why does it do this?
There is nothing wrong with this: ASCII code of
xis 120 in decimal. The'x'syntax is a convenient syntax for entering small integer constants using their representation as ASCII characters; other than the syntax, constants in single quotes are regular integers.If you would like the number re-interpreted as a character, use the corresponding format specifier in the
NSLogcall, and get thecharvalue from theNSNumberobject: