NSNumber可以存储的最大值是多少? [英] What's the largest value an NSNumber can store?
问题描述
NSNumber可以存储的最大值是多少?
What's the largest value an NSNumber can store?
// ok
NSNumber *value = @(1 << 31);
// gives compiler error, so max NSNumber is 32-bit uint?
NSNumber *value = @(1 << 32);
推荐答案
NSNumber is actually a class cluster, meaning that when you create an instance you may be getting any of a variety of concrete subclasses, each capable of storing a different kind of numeric type. The actual types available, and their sizes, may be machine-dependent.
查看 NSNumber文档向您显示可以存储的不同类型的数字:两个最大的整数选项为 +numberWithInteger:
(或+numberWithUnsignedInteger:
),其中存储了
Looking at the NSNumber documentation shows you the different kinds of numbers you can store: the two largest integer options would be +numberWithLongLong:
(or +numberWithUnsignedLongLong:
), which stores a long long
, and +numberWithInteger:
(or +numberWithUnsignedInteger:
), which stores an NSInteger. The maximum NSNumber values are therefore limited by these types.
基金会文档指出:
在构建32位应用程序时,NSInteger是32位整数. 64位应用程序将NSInteger视为64位整数.
When building 32-bit applications, NSInteger is a 32-bit integer. A 64-bit application treats NSInteger as a 64-bit integer.
编译器很聪明,将创建与数字文字相同类型的NSNumber.如上面的注释中所述,如果计算机的unsigned long long
类型具有超过32位,则可以使用@(1ULL << 32)
.
The compiler is smart and will create an NSNumber of the same type as your numeric literal. As mentioned in the comments above, you can use @(1ULL << 32)
if your machine has an unsigned long long
type with more than 32 bits.
此外,NSNumber是免费的桥接到CFNumber的电话,这意味着您可以试用数字类型" 部分.您将看到这些基本上与NSNumber选项相同.
Furthermore, NSNumber is toll-free bridged to CFNumber, meaning you can try out functions like CFNumberGetByteSize()
for yourself — and have a look at the Number Types section of the CFNumber documentation. You'll see these are basically the same as the NSNumber options.
此外,NSDecimalNumber类是NSNumber的子类,它提供
Additionally, the NSDecimalNumber class, a subclass of NSNumber, provides the +maximumDecimalNumber
method which you can use to find the maximum value that can be stored in an NSDecimalNumber. NSDecimalNumber, and the floating-point types, may be able to store bigger numbers than the integer types, though with decreasing precision.
这篇关于NSNumber可以存储的最大值是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!