NSNumber可以用来代表C语言的任何基础数据类型,如int, double, char, float, long, short,及其被unsigned修饰时的各类型,以及OC中的BOOL型。
1,基础数据类型 ------> NSNumber :
- NSNumber *magicNumber = [[NSNumber alloc] initWithInt:42];
- NSNumber *unsignedNumber = [[NSNumber alloc] initWithUnsignedInt:42u];
- NSNumber *longNumber = [[NSNumber alloc] initWithLong:42l];
- NSNumber *boolNumber = [[NSNumber alloc] initWithBOOL:YES];
- NSNumber *simpleFloat = [NSNumber numberWithFloat:3.14f];
- NSNumber *betterDouble = [NSNumber numberWithDouble:3.1415926535];
- NSNumber *someChar = [NSNumber numberWithChar:'T'];
复制代码
及其简便版:
- NSNumber *magicNumber = @42;
- NSNumber *unsignedNumber = @42u;
- NSNumber *longNumber = @42l;
- NSNumber *boolNumber = @YES;
- NSNumber *simpleFloat = @3.14f;
- NSNumber *betterDouble = @3.1415926535;
- NSNumber *someChar = @'T';
复制代码
2,NSNumber -----> 基础数据类型
int scalarMagic = [magicNumber intValue];
unsigned int scalarUnsigned = [unsignedNumber unsignedIntValue];
long scalarLong = [longNumber longValue];
BOOL scalarBool = [boolNumber boolValue];
float scalarSimpleFloat = [simpleFloat floatValue];
double scalarBetterDouble = [betterDouble doubleValue];
char scalarChar = [someChar charValue];
|
|