NSNumber各类型包装转换
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);
NSNumber各类型包装转换相关推荐
- [改善Java代码]避开基本类型数组转换列表陷阱
开发中经常用到Arrays和Collections这两个工具类. 在数组和列表之间进行切换.非常方便.但是也会遇到一些问题. 看代码: import java.util.Arrays; import ...
- java 基本类型 包装类型_Java中基本类型和包装类
基本类型运算 boolean类型数据可以进行逻辑运算(&&,||,!),其他的基本类型都可以进行数值计算(+,-,*,/).逻辑运算比较简单易懂,完全与逻辑数学的规则一致,而数值运算涉 ...
- Go 学习笔记(32)— 类型系统(命名类型、未命名类型、底层类型、类型强制转换、类型别名和新声明类型)
1. 命名类型和未命名类型 1.1 命名类型 类型可以通过标识符来表示,这种类型称为命名类型( Named Type ). Go 语言的基本类型中有 20 个预声明简单类型都是命名类型, Go 语言还 ...
- 【C#串口编程计划】通信协议解析 -- byte[]与常用类型的转换
刚刚完成一个串口通讯的系统.目前在把串口通信的代码整合到团队的类库中(把串口通信与网口Soket通讯整合起来,后面只需要配置参数,就可实现网络与串口通讯的转换),故C#串口编程计划的最后一篇图文&qu ...
- c++11 字符串与int类型的转换
转 字符串与其他基本类型的转换--从C到C++11 发表于1年前(2014-09-19 14:07) 阅读(822) | 评论(2) 7人收藏此文章, 我要收藏 赞1 12月12日北京OSC源创会 ...
- CString与LPCWSTR、LPSTR、char*、LPWSTR等类型的转换【转】
CString与LPCWSTR.LPSTR.char*.LPWSTR等类型的转换 VC++ 2010-09-25 21:23:12 阅读457 评论3 字号:大中小 订阅 一.CString与LP ...
- Linq--使用Linq在不同类型之间转换
需求: 有两个类型,分别为MyUser和YourUser,要在这两个类型之间进行转换 MyUser.cs namespace XMLSpy.Linq{ public class MyUser{ ...
- Java中number数字类型的转换_Java下数字类型的转换 (转)
Java下数字类型的转换 (转)[@more@] 作者:家居猫 各种数字类型转换成字符串型: String s = String.valueOf( value); // 其中 value 为任意一种数 ...
- C++(20)--类型自动转换
类型自动转换 1.C++内置类型转换 2.实现自定义类的类型转换 <老九学堂C++课程><C++ primer>学习笔记.<老九学堂C++课程>详情请到B站搜索&l ...
最新文章
- ConcurrentHashMap介绍
- [导入]请给软件企业新员工推荐书籍
- 成为自信的node.js开发者(一)
- 剑指Offer - 面试题66. 构建乘积数组(正反遍历)
- etmvc mysql乱码_Etmvc学习文档
- js实现文字从右滚动到左边代码循环滚动实例
- devops实践指南_DevOps:掌握这些便捷指南
- 算天数什么时候加一什么时候不加一_陌陌加公会不加公会的区别?
- AD09由英文改中文菜单步骤
- 硬盘读写性能iozone测试方法及下载
- zen-cart修改 zencart 模板修改
- 怎么用c语言做自动回复消息,【微信开发学习笔记】01消息自动回复关键词自动回复...
- 人工智能在智慧城市建设的应用场景
- 联想模拟器安装激活面具magisk教程
- linux-命令简写——归原
- 基于android的理财软件,基于Android的理财系统APP的设计
- 一文搞定BP神经网络——从原理到应用(原理篇)
- 简约至上:你必须知道的产品设计
- 教你如何在电脑上建一个绝密文件夹
- 实验室设计如何搭配颜色色彩
热门文章
- 中国网民近一年因垃圾诈骗信息等遭受经济损失915亿
- 刚刚,韦布太空望远镜成功“睁眼”!主镜展开飞向拉格朗日点,网友:百亿美元偷窥外星设备开工大吉...
- 视频教程- Linux命令技巧及黑客防御实战-Linux
- 怎么加快计算机运行速度,如何加快电脑运行速度?让电脑速度翻一倍方法
- mongoose中通过-id查询的方法
- 7.21 给张孝祥老师的第一封信
- 看数据达人如何用一句话解读大数据
- 开启吉利动力4.0时代 全球动力科技品牌“雷神动力”正式发布
- android 屏幕时间锁,何同学时间锁壁纸
- 公众号一键拨打400服务电话