NSNumber
An object wrapper for primitive scalar numeric values.
Declaration
class NSNumberMentioned in
Overview
NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char, short int, int, long int, long long int, float, or double or as a BOOL. (Note that number objects do not necessarily preserve the type they are created with.) It also defines a compare(_:) method to determine the ordering of two NSNumber objects.
NSNumber is “toll-free bridged” with its Core Foundation counterparts: CFNumber for integer and floating point values, and CFBoolean for Boolean values. See Toll-Free Bridging for more information on toll-free bridging.
Value Conversions
NSNumber provides readonly properties that return the object’s stored value converted to a particular Boolean, integer, unsigned integer, or floating point C scalar type. Because numeric types have different storage capabilities, attempting to initialize with a value of one type and access the value of another type may produce an erroneous result—for example, initializing with a double value exceeding FLT_MAX and accessing its floatValue, or initializing with an negative integer value and accessing its uintValue. In some cases, attempting to initialize with a value of a type and access the value of another type may result in loss of precision—for example, initializing with a double value with many significant digits and accessing its floatValue, or initializing with a large integer value and accessing its int8Value.
An NSNumber object initialized with a value of a particular type accessing the converted value of a different kind of type, such as unsigned int and float, will convert its stored value to that converted type in the following ways:
| ||||
|---|---|---|---|---|
|
|
| ||
|
|
|
| ||||
|---|---|---|---|---|
|
|
|
| |
|
|
|
| |
|
| invalid, erroneous result |
|
| ||||
|---|---|---|---|---|
|
|
|
| |
|
|
|
|
| ||||
|---|---|---|---|---|
|
|
|
| |
|
|
|
| |
|
| invalid, erroneous result |
|
Subclassing Notes
As with any class cluster, subclasses of NSNumber must override the primitive methods of its superclass, NSValue. In addition, there are two requirements around the data type your subclass represents:
Your implementation of objCType must return one of “
c”, “C”, “s”, “S”, “i”, “I”, “l”, “L”, “q”, “Q”, “f”, and “d”. This is required for the other methods of NSNumber to behave correctly.Your subclass must override the accessor method that corresponds to the declared type—for example, if your implementation of objCType returns “
i”, you must override int32Value.
Topics
Initializing an NSNumber Object
init(value:)init(value:)init(value:)init(value:)init(value:)init(value:)init(value:)init(value:)init(value:)init(value:)init(value:)init(value:)init(value:)
Accessing Numeric Values
boolValueint8ValuedecimalValuedoubleValuefloatValueint32ValueintValueint64Valueint16Valueuint8ValueuintValueuint32Valueuint64Valueuint16Value
Retrieving String Representations
Comparing NSNumber Objects
Number Validation
Initializers
init(bool:)init(char:)init(coder:)init(double:)init(float:)init(int:)init(integer:)init(longLong:)init(short:)init(unsignedChar:)init(unsignedInt:)init(unsignedInteger:)init(unsignedLongLong:)init(unsignedShort:)