A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yanggangcl 中级黑马   /  2015-11-4 22:43  /  629 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

NSNumber可以用来代表C语言的任何基础数据类型,如int, double, char, float, long, short,及其被unsigned修饰时的各类型,以及OC中的BOOL型。
1,基础数据类型 ------> NSNumber :
  1. NSNumber *magicNumber = [[NSNumber alloc] initWithInt:42];
  2. NSNumber *unsignedNumber = [[NSNumber alloc] initWithUnsignedInt:42u];
  3. NSNumber *longNumber = [[NSNumber alloc] initWithLong:42l];
  4. NSNumber *boolNumber = [[NSNumber alloc] initWithBOOL:YES];
  5. NSNumber *simpleFloat = [NSNumber numberWithFloat:3.14f];
  6. NSNumber *betterDouble = [NSNumber numberWithDouble:3.1415926535];
  7. NSNumber *someChar = [NSNumber numberWithChar:'T'];
复制代码

及其简便版:
  1. NSNumber *magicNumber = @42;
  2. NSNumber *unsignedNumber = @42u;
  3. NSNumber *longNumber = @42l;
  4. NSNumber *boolNumber = @YES;
  5. NSNumber *simpleFloat = @3.14f;
  6. NSNumber *betterDouble = @3.1415926535;
  7. 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];

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马