NSNumber各类型包装转换
2016-03-08 01:50
369 查看
1)int-->NSNumber:numberWithInt
2)NSNumber-->nsinteger:integerValue
3)string -->double:initWithString
4)CGFloat --> dobule:initWithFloat,decimalobj doubleValue
5)使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
6)NSInteger是基础类型,但是NSNumber是一个类。如果想要在NSMutableArray里存储一个数值,直接用NSInteger是不行的,比如在一个NSMutableArray里面.
7) NSString与NSInteger的相互转换
NSString * string = [NSString stringWithFormat:@"%d",integerNumber];
integer = [string intValue];
static void numberTest(){
NSNumber *numObj = [NSNumber numberWithInt: 2];
NSLog(@"numObj=%@",numObj);
NSInteger myInteger = [numObj integerValue];
NSLog(@"myInteger=%d",myInteger);
int a = [numObj intValue];
NSLog(@"a=%d",a);
//浮点数值使用CGFloat,NSDecimalNumber对象进行处理:
NSDecimalNumber *myDecimalObj = [[NSDecimalNumber alloc] initWithString:@"23.30"];
NSLog(@"myDecimalObj doubleValue=%6.3f",[myDecimalObj doubleValue]);
CGFloat myCGFloatValue = 43.4;
NSDecimalNumber *myOtherDecimalObj = [[NSDecimalNumber alloc] initWithFloat:myCGFloatValue];
NSLog(@"myOtherDecimalObj doubleValue=%6.5f",[myOtherDecimalObj doubleValue]);
}
2 、C语言的基本数据类型长度
NSLog(@"The size of an int is: %lu bytes.",sizeof(int));
NSLog(@"The size of a short int is: %lu bytes.",sizeof(short int));
NSLog(@"The size of a long int is: %lu bytes.",sizeof(long int));
NSLog(@"The size of a char is: %lu bytes.",sizeof(char));
NSLog(@"The size of a float is: %lu bytes.",sizeof(float));
NSLog(@"The size of a double is: %lu bytes.",sizeof(double));
NSLog(@"The size of a bool is: %lu bytes.",sizeof(bool)); // Do any additional setup after loading the view,
结果:
2012-06-13 13:55:46.726 BaseType[3032:f803] The size of an int is: 4 bytes.
2012-06-13 13:55:46.726 BaseType[3032:f803] The size of a short int is: 2 bytes.
2012-06-13 13:55:46.727 BaseType[3032:f803] The size of a long int is: 4 bytes.
2012-06-13 13:55:46.731 BaseType[3032:f803] The size of a char is: 1 bytes.
2012-06-13 13:55:46.732 BaseType[3032:f803] The size of a float is: 4 bytes.
2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a double is: 8 bytes.
2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a bool is: 1 bytes.
3、格式化输出数据
//整型
int integerType = 5;
//浮点型
float floatType = 3.1415;
//双浮点型
double doubleType = 2.2033;
//短整型
short int shortType = 200;
//长整型
long long int longlongType = 7758123456767L;
//c语言字符串
char * cstring = "this is a string!";
//整型
NSLog(@"The value of integerType = %d",integerType);
//浮点型
NSLog(@"The value of floatType = %.2f",floatType);
//双浮点型
NSLog(@"The value of doubleType = %e",doubleType);
//短整型
NSLog(@"The value of shortType = %hi",shortType);
//长整型
NSLog(@"The value of longlongType = %lli",longlongType);
//c语言字符串
NSLog(@"The value of cstring = %s",cstring);
结果:
2012-06-13 14:06:18.757 BaseType[3215:f803] The value of integerType = 5
2012-06-13 14:06:18.757 BaseType[3215:f803] The value of floatType = 3.14
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of doubleType = 2.203300e+00
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of shortType = 200
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of longlongType = 7758123456767
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of cstring = this is a string!
4、 int,NSInteger,NSUInteger,NSNumber
1.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
2.NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
3.有人说既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。
NSInteger是基础类型,但是NSNumber是一个类。如果想要在NSMutableArray里存储一个数值,直接用NSInteger是不行的,比如在一个NSMutableArray里面这样用:
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:[NSNumber numberWithInt:88]];
这样是会引发编译错误的,因为NSMutableArray里面放的需要是一个类,但‘88’不是类。
Cocoa提供了NSNumber类来包装(即以对象形式实现)基本数据类型。
例如以下创建方法:
+ (NSNumber *) numberWithChar: (char) value;
+ (NSNumber *) numberWithInt: (int) value;
+ (NSNumber *) numberWithFloat: (float) value;
+ (NSNumber *) numberWithBool: (BOOL) value;
将基本类型数据封装到NSNumber中后,就可以通过下面的实例方法重新获取它:
- (char) charValue;
- (int) intValue;
- (float) floatValue;
- (BOOL) boolValue;
- (NSString *) stringValue;
例子:
NSNumber *num = [NSNumber numberWithInt:88];
NSInteger integer = [num intValue];
5、NSString与NSInteger的相互转换
NSInteger integerNumber = 888;
NSString * string = [NSString stringWithFormat:@"%d",integerNumber];
NSLog(@"string is %@", string);
integer = [string intValue];
NSLog(@"integer is%d", integerNumber);
转自大树blog 点击打开链接
2)NSNumber-->nsinteger:integerValue
3)string -->double:initWithString
4)CGFloat --> dobule:initWithFloat,decimalobj doubleValue
5)使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
6)NSInteger是基础类型,但是NSNumber是一个类。如果想要在NSMutableArray里存储一个数值,直接用NSInteger是不行的,比如在一个NSMutableArray里面.
7) NSString与NSInteger的相互转换
NSString * string = [NSString stringWithFormat:@"%d",integerNumber];
integer = [string intValue];
static void numberTest(){
NSNumber *numObj = [NSNumber numberWithInt: 2];
NSLog(@"numObj=%@",numObj);
NSInteger myInteger = [numObj integerValue];
NSLog(@"myInteger=%d",myInteger);
int a = [numObj intValue];
NSLog(@"a=%d",a);
//浮点数值使用CGFloat,NSDecimalNumber对象进行处理:
NSDecimalNumber *myDecimalObj = [[NSDecimalNumber alloc] initWithString:@"23.30"];
NSLog(@"myDecimalObj doubleValue=%6.3f",[myDecimalObj doubleValue]);
CGFloat myCGFloatValue = 43.4;
NSDecimalNumber *myOtherDecimalObj = [[NSDecimalNumber alloc] initWithFloat:myCGFloatValue];
NSLog(@"myOtherDecimalObj doubleValue=%6.5f",[myOtherDecimalObj doubleValue]);
}
2 、C语言的基本数据类型长度
NSLog(@"The size of an int is: %lu bytes.",sizeof(int));
NSLog(@"The size of a short int is: %lu bytes.",sizeof(short int));
NSLog(@"The size of a long int is: %lu bytes.",sizeof(long int));
NSLog(@"The size of a char is: %lu bytes.",sizeof(char));
NSLog(@"The size of a float is: %lu bytes.",sizeof(float));
NSLog(@"The size of a double is: %lu bytes.",sizeof(double));
NSLog(@"The size of a bool is: %lu bytes.",sizeof(bool)); // Do any additional setup after loading the view,
结果:
2012-06-13 13:55:46.726 BaseType[3032:f803] The size of an int is: 4 bytes.
2012-06-13 13:55:46.726 BaseType[3032:f803] The size of a short int is: 2 bytes.
2012-06-13 13:55:46.727 BaseType[3032:f803] The size of a long int is: 4 bytes.
2012-06-13 13:55:46.731 BaseType[3032:f803] The size of a char is: 1 bytes.
2012-06-13 13:55:46.732 BaseType[3032:f803] The size of a float is: 4 bytes.
2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a double is: 8 bytes.
2012-06-13 13:55:46.733 BaseType[3032:f803] The size of a bool is: 1 bytes.
3、格式化输出数据
//整型
int integerType = 5;
//浮点型
float floatType = 3.1415;
//双浮点型
double doubleType = 2.2033;
//短整型
short int shortType = 200;
//长整型
long long int longlongType = 7758123456767L;
//c语言字符串
char * cstring = "this is a string!";
//整型
NSLog(@"The value of integerType = %d",integerType);
//浮点型
NSLog(@"The value of floatType = %.2f",floatType);
//双浮点型
NSLog(@"The value of doubleType = %e",doubleType);
//短整型
NSLog(@"The value of shortType = %hi",shortType);
//长整型
NSLog(@"The value of longlongType = %lli",longlongType);
//c语言字符串
NSLog(@"The value of cstring = %s",cstring);
结果:
2012-06-13 14:06:18.757 BaseType[3215:f803] The value of integerType = 5
2012-06-13 14:06:18.757 BaseType[3215:f803] The value of floatType = 3.14
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of doubleType = 2.203300e+00
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of shortType = 200
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of longlongType = 7758123456767
2012-06-13 14:06:18.758 BaseType[3215:f803] The value of cstring = this is a string!
4、 int,NSInteger,NSUInteger,NSNumber
1.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
2.NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
[align=center][/align] |
NSInteger是基础类型,但是NSNumber是一个类。如果想要在NSMutableArray里存储一个数值,直接用NSInteger是不行的,比如在一个NSMutableArray里面这样用:
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:[NSNumber numberWithInt:88]];
这样是会引发编译错误的,因为NSMutableArray里面放的需要是一个类,但‘88’不是类。
Cocoa提供了NSNumber类来包装(即以对象形式实现)基本数据类型。
例如以下创建方法:
+ (NSNumber *) numberWithChar: (char) value;
+ (NSNumber *) numberWithInt: (int) value;
+ (NSNumber *) numberWithFloat: (float) value;
+ (NSNumber *) numberWithBool: (BOOL) value;
将基本类型数据封装到NSNumber中后,就可以通过下面的实例方法重新获取它:
- (char) charValue;
- (int) intValue;
- (float) floatValue;
- (BOOL) boolValue;
- (NSString *) stringValue;
例子:
NSNumber *num = [NSNumber numberWithInt:88];
NSInteger integer = [num intValue];
5、NSString与NSInteger的相互转换
NSInteger integerNumber = 888;
NSString * string = [NSString stringWithFormat:@"%d",integerNumber];
NSLog(@"string is %@", string);
integer = [string intValue];
NSLog(@"integer is%d", integerNumber);
转自大树blog 点击打开链接
相关文章推荐
- Objective-C的内省(Introspection)用法小结
- Objective-C中常用的结构体NSRange,NSPoint,NSSize(CGSize),NSRect实例分析
- Objective-C中使用NSString类操作字符串的方法小结
- Objective-C中NSNumber与NSDictionary的用法简介
- Objective-C中NSLog输出格式大全
- 全面解析Objective-C中的block代码块的使用
- Swift调用Objective-C编写的API实例
- Swift、Objective-C、Cocoa混合编程设置指南
- Objective-c代码如何移植为Swift代码 Objective-c代码转移到Swift过程介绍
- Swift调用Objective-C代码
- 以实例讲解Objective-C中的KVO与KVC机制
- 简介Objective-C解析XML与JSON数据格式的方法
- 浅析Objective-C中分类Category的使用
- 理解Objective-C的变量以及面相对象的继承特性
- 简单讲解Objective-C的基本特性及其内存管理方式
- Objective-C处理空字符串和页面传值及自定义拷贝
- objective-c中生成随机数的方法
- 在Swift中使用Objective-C编写类、继承Objective-C类
- Swift能代替Objective-C吗?
- Objective-C 消息传递机制详解