int vs NSNumber vs NSInteger [英] int vs NSNumber vs NSInteger
问题描述
我有一行代码将根据数据类型day和1的不同而有所不同。我相信以下内容虽然我稍后会检查我的源代码。
I have a line of code that will work differently depending on the datatypes "day" and "1". I believe it is the following although I will check my source code later.
day = day + 1;
这有意义吗?差异会是什么?
Does this make sense? What would the differences be?
推荐答案
NSInteger
是一个类型定义,描述一个整数 - 但它不等同于64位平台上的 int
。
您可以通过cmd单击<$ c来检查typedef Xcode中的$ c> NSInteger 。
NSInteger
定义为 int
构建32位应用程序时和64位应用程序的 long
。
大多数情况下你可以用NSInteger替换int,但是这样做有一些事情要考虑。
Apple的64位Cocoa过渡指南提供了一些相关信息。
NSInteger
is a type definition that describes an integer - but it is NOT equivalent to int
on 64-bit platforms.
You can examine the typedef by cmd-clicking on NSInteger
in Xcode.
NSInteger
is defined as int
when building a 32-bit app and as long
for 64-bit apps.
Most of the time you can replace int with NSInteger, but there are some things to consider when doing so.
Apple's 64-Bit Transition Guide for Cocoa has some information about that.
NSNumber 是一个帮助您将数字类型存储为对象的类。它具有在不同类型和方法之间进行转换的方法,以检索数值的字符串表示。
如果使用类型<$的变量 day
c $ c> NSNumber * 您在示例中所做的方式,您没有修改 day
的值,而是修改其内存地址。
NSNumber is a class that helps you to store numeric types as object. It has methods to convert between different types and methods to retrieve a string representation of your numeric value.
If you use a variable day
of type NSNumber*
the way you did in your example, you are not modifying the value of day
but its memory address.
如果您正在处理时间值,您还可以查看 NSDate 。
If you are working with time values you also could take a look at NSDate.
这篇关于int vs NSNumber vs NSInteger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!