ios int vs NSNumber vs NSInteger
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3032028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
int vs NSNumber vs NSInteger
提问by Moshe
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”和“1”的不同而不同。我相信它如下,尽管我稍后会检查我的源代码。
day = day + 1;
day = day + 1;
Does this make sense? What would the differences be?
这有意义吗?差异会是什么?
回答by Thomas Zoechling
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 Cocoahas some information about that.
NSInteger
是描述整数的类型定义 - 但它不等同int
于 64 位平台。
您可以通过NSInteger
在 Xcode 中单击 cmd 来检查 typedef 。NSInteger
定义为int
构建 32 位应用程序和long
64 位应用程序时。
大多数情况下,您可以将 int 替换为 NSInteger,但这样做时需要考虑一些事项。
Apple 的 Cocoa 64-Bit Transition Guide 提供了一些相关信息。
NSNumberis 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.
NSNumber是一个类,可以帮助您将数字类型存储为对象。它具有在不同类型之间转换的方法和检索数值的字符串表示的方法。
如果您像在示例中那样使用day
类型变量NSNumber*
,那么您不是在修改 的值,day
而是在修改其内存地址。
If you are working with time values you also could take a look at NSDate.
如果您正在处理时间值,您还可以查看NSDate。
回答by Jawad Zeb
NSInteger
is a simply an integer, NSNumber
is an object where as int
is a primitive data type.
NSInteger
是一个简单的整数,NSNumber
是一个对象,其中 asint
是原始数据类型。
NSInteger is nothing more than a synonym for a long integer.
NSInteger 只不过是长整数的同义词。
If you need to store a number somewhere, use NSNumber. If you're doing calculations, loops, etc, use NSInteger, NSUInteger or int.
如果需要在某处存储数字,请使用 NSNumber。如果您正在执行计算、循环等操作,请使用 NSInteger、NSUInteger 或 int。
You can wrap an NSInteger into an NSNumber using:
您可以使用以下方法将 NSInteger 包装到 NSNumber 中:
NSNumber *NumberToStore = @(21);
and get it back using:
并使用:
NSInteger retrievingInteger = [NumberToStore integerValue];
回答by walkytalky
1
is a literal and the compiler will make it the appropriate numeric type if that's appropriate.
1
是一个文字,如果合适,编译器会使其成为合适的数字类型。
Note, however, that while NSInteger
and int
are to most intents and purposes the same thing, NSNumber
is an object type. For that case, your code doesn't make sense (and shouldn't compile without warnings).
但是请注意,对于大多数意图和目的而言,whileNSInteger
和while是一种对象类型。在这种情况下,您的代码没有意义(并且不应在没有警告的情况下进行编译)。int
NSNumber
(Actually, there are some circumstances where it would make a kind of sense, in terms of pointer arithmetic, but that's totally not what you want.)
(实际上,就指针运算而言,在某些情况下它会有意义,但这完全不是您想要的。)
For the NSNumber case, you'd instead want something like:
对于 NSNumber 的情况,你会想要类似的东西:
day = [NSNumber numberWithInt:[day intValue] + 1];
回答by kubi
NSInteger
and int
are equivalent, NSNumber
is an object used to store primitive number types (int, long, float, etc.).
NSInteger
并且int
是等效的,NSNumber
是一个用于存储原始数字类型(int、long、float 等)的对象。
The example code you posted will work just fine if day
is an NSinteger
, it won't work at all if it's an NSNumber
.
您发布的示例代码如果day
是NSinteger
就可以正常工作,如果是NSNumber
.