Counting on your fingers—integral numbers

An integer is a whole number that can be negative or positive.The values 27,-5,and 0 are all valid integer values,but 0.82 isn't because it contains a decimal point.

 

By default,variables of type int are signed and can represent both positive and negative values.有时,你若想限制一个整型变量只能存正整数,那就在类型前加一个unsigned,例如:

unsigned int a;

当然,你也可以显式创建一个signed integer变量,用signed,如signed int a;

 

By default,integer constants are specified in decimal(十进制),or base 10,which is the most familiar notation for most people.

 

In an ideal world,developers wouldn't have any constraint on the value of integers——they could be infinitely high or low——but,unfortunately,constraints do exist.

 

在Objective-C中,int是变量以及参数的默认类型。这意味着你可以去掉变量声明中的int,并且在大多数情况下,这仍然是可以编译的。因此下面2句变量声明是等价的:

unsigned a;

unsigned int a;

 

As you explore Cocoa Touch,you'll find that most APIs use data types with names such as NSInteger or NSUInteger instead of int and unsigned int.These additional types

are part of Apple's progress toward 64-bit computing.

 

当前,所有的iOS-powered设备(以及旧版本的Mac OS X)用的是ILP32编程模型,支持32位地址空间。从Mac OS X 10.4开始,桌面开始移向LP64编程模型,也就是支持64位

地址空间,在此模型中,long int类型的变量以及内存地址在大小上增加到了64位(在32位编程模型中,long int仍然是32位),不过,其他所有原始类型,如int,还是保持原样。

 

作为充分利用64位平台努力的一部分,Cocoa引入了NSInteger数据类型,它在32位平台上是32位整数,当把同样的源码在64位平台上编译,就会增加至64位。

 

NSInteger exists for use in APIs that should be 64-bit integers on 64-bit platforms but for one reason or another must be typed as int instead of long int on 32-bit platforms.

 

如果,你想要你的源码适用于未来,那就用NSInteger和NSUIntger这样的类型来声明变量。尽管,为iPhone编译的时候,它们等价于int和unsigned int,但谁知道以后,iPhone或

iPad将会变成一个64位设备呢,或者,你会想要在一些匹配的桌面应用中重用你的源码。

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值