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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:19:59  来源:igfitidea点击:

int vs NSNumber vs NSInteger

iphoneobjective-cios

提问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

NSIntegeris a type definition that describes an integer - but it is NOT equivalent to inton 64-bit platforms.
You can examine the typedef by cmd-clicking on NSIntegerin Xcode.
NSIntegeris defined as intwhen building a 32-bit app and as longfor 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 位应用程序和long64 位应用程序时。
大多数情况下,您可以将 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 dayof type NSNumber*the way you did in your example, you are not modifying the value of daybut 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

NSIntegeris a simply an integer, NSNumberis an object where as intis 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

1is a literal and the compiler will make it the appropriate numeric type if that's appropriate.

1是一个文字,如果合适,编译器会使其成为合适的数字类型。

Note, however, that while NSIntegerand intare to most intents and purposes the same thing, NSNumberis an object type. For that case, your code doesn't make sense (and shouldn't compile without warnings).

但是请注意,对于大多数意图和目的而言,whileNSInteger和while是一种对象类型。在这种情况下,您的代码没有意义(并且不应在没有警告的情况下进行编译)。intNSNumber

(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

NSIntegerand intare equivalent, NSNumberis 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 dayis an NSinteger, it won't work at all if it's an NSNumber.

您发布的示例代码如果dayNSinteger就可以正常工作,如果是NSNumber.