C语言int与interger,NSInteger与int类型区别

1.在iOS开发中我们比较喜欢使用int类型的变量,可是你仔细研究过C语言中的int和OC中int的区别了吗?

下面我们来分析下:

查到c语言中,int和long的字节数是和操作系统指针所占位数相等。

但c语言中说,long的长度永远大于或等于int

Objective-C里,苹果的官方文档中总是推荐用NSInteger

原来在苹果的api实现中,NSInteger是一个封装,它会识别当前操作系统的位数,自动返回最大的类型。

定义的代码类似于下:

if LP64 ||

TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 ||

NS_BUILD_32_LIKE_64

typedef long NSInteger;

typedef unsigned long NSUInteger;

else

typedef int NSInteger;

typedef unsigned int NSUInteger;

endif

You usually want to use NSInteger when

you don't know what kind of processor architecture your code might

run on, so you may for some reason want the largest possible int

type, which on 32 bit systems is just an int, while on a 64-bit

system it's a long.

总结:NSInteger与int的区别是NSInteger会根据系统的位数(32or64)自动选择int的最大数值(int or

long)。

2、

NSInteger type;

type=1;

NSString *Type=[NSString stringWithFormat:@"%zi",type];

总结下:

"%zi"是OC特有的格式化输入输出控制字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值