What's the largest value an NSNumber can store?
max, nsnumber, objective-c, primitive
Solution
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.
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.
The Foundation documentation states:
When building 32-bit applications, NSInteger is a 32-bit integer. A 64-bit application treats NSInteger as a 64-bit integer.
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.
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.
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.
Problem
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); ```