- Inherits from:
- NSValue : NSObject
- Conforms to:
- NSCoding
- (NSValue)
- NSCopying (NSValue)
- NSObject (NSObject)
Declared in:
- Foundation/NSValue.h
- Foundation/NSDecimalNumber.h
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
.
It also defines a compare: method to
determine the ordering of two NSNumber objects.
An NSNumber records the numeric type it's created with, and uses the C rules for numeric conversion when comparing NSNumbers of different numeric types and when returning values as C numeric types. See any standard C reference for information on type conversion.
As with any class cluster, if you create a subclass of NSNumber, you have to override the primitive methods of its superclass, NSValue. Furthermore, there is a restricted set of return values that your implementation of the NSValue method objCType can return, in order to take advantage of the abstract implementations of the non-primitive methods. The valid return values are "c", "C", "s", "S", "i", "I", "l", "L", "q", "Q", "f", and "d".
- Creating an NSNumber
- + numberWithBool:
- + numberWithChar:
- + numberWithDouble:
- + numberWithFloat:
- + numberWithInt:
- + numberWithLong:
- + numberWithLongLong:
- + numberWithShort:
- + numberWithUnsignedChar:
- + numberWithUnsignedInt:
- + numberWithUnsignedLong:
- + numberWithUnsignedLongLong:
- + numberWithUnsignedShort:
- - initWithBool:
- - initWithChar:
- - initWithDouble:
- - initWithFloat:
- - initWithInt:
- - initWithLong:
- - initWithLongLong:
- - initWithShort:
- - initWithUnsignedChar:
- - initWithUnsignedInt:
- - initWithUnsignedLong:
- - initWithUnsignedLongLong:
- - initWithUnsignedShort:
- Accessing numeric values
- - boolValue
- - charValue
- - decimalValue
- - descriptionWithLocale:
- - doubleValue
- - floatValue
- - intValue
- - longLongValue
- - longValue
- - shortValue
- - stringValue
- - unsignedCharValue
- - unsignedIntValue
- - unsignedLongLongValue
- - unsignedLongValue
- - unsignedShortValue
- Comparing NSNumbers
- - compare:
- - isEqualToNumber:
+ (NSNumber *)numberWithBool:(BOOL)value
BOOL
.+ (NSNumber *)numberWithChar:(char)value
char
.+ (NSNumber *)numberWithDouble:(double)value
double
.+ (NSNumber *)numberWithFloat:(float)value
float
.+ (NSNumber *)numberWithInt:(int)value
int
.+ (NSNumber *)numberWithLong:(long)value
long
.+ (NSNumber *)numberWithLongLong:(long
long)value
long long
.+ (NSNumber *)numberWithShort:(short)value
short
.+ (NSNumber *)numberWithUnsignedChar:(unsigned
char)value
unsigned char
.+ (NSNumber *)numberWithUnsignedInt:(unsigned
int)value
unsigned int
.+ (NSNumber *)numberWithUnsignedLong:(unsigned
long)value
unsigned long
.+ (NSNumber *)numberWithUnsignedLongLong:(unsigned
long long)value
unsigned long long
.+ (NSNumber *)numberWithUnsignedShort:(unsigned
short)value
unsigned short
.- (BOOL)boolValue
BOOL
,
converting it as necessary. Note: The value returned by this method isn't guaranteed to be one of YES or NO. A zero value always means NO or false, but any nonzero value should be interpreted as YES or true. |
- (char)charValue
char
, converting
it as necessary.- (NSComparisonResult)compare:(NSNumber
*)aNumber
NSOrderedAscending
if aNumber's
value is greater than the receiver's, NSOrderedSame
if they're
equal, and NSOrderedDescending
if aNumber's
value is less than the receiver's.compare: follows the standard C rules for type conversion. For example, if you compare an NSNumber that has an integer value with an NSNumber that has a floating point value, the integer value is converted to a floating point value for comparison.
- (NSDecimal)decimalValue
float
and double
values.- (NSString *)descriptionWithLocale:(NSDictionary
*)aLocale
To obtain the string representation, this method invokes NSString's initWithFormat:locale: method, supplying the format based on the type the NSNumber was created with:
Data Type | Format Specification |
char | %i |
double | %0.16g |
float | %0.7g |
int | %i |
long | %li |
long long | %li |
short | %hi |
unsigned char | %u |
unsigned int | %u |
unsigned long | %lu |
unsigned long long | %lu |
unsigned short | %hu |
See Also: - stringValue
- (double)doubleValue
double
, converting
it as necessary.- (float)floatValue
float
, converting
it as necessary.- (id)initWithBool:(BOOL)value
BOOL
.- (id)initWithChar:(char)value
char
.- (id)initWithDouble:(double)value
double
.- (id)initWithFloat:(float)value
float
.- (id)initWithInt:(int)value
int
.- (id)initWithLong:(long)value
long
.- (id)initWithLongLong:(long
long)value
long long
.- (id)initWithShort:(short)value
short
.- (id)initWithUnsignedChar:(unsigned
char)value
unsigned char
.- (id)initWithUnsignedInt:(unsigned
int)value
unsigned int
.- (id)initWithUnsignedLong:(unsigned
long)value
unsigned long
.- (id)initWithUnsignedLongLong:(unsigned
long long)value
unsigned long long
.- (id)initWithUnsignedShort:(unsigned
short)value
unsigned short
.- (int)intValue
int
, converting
it as necessary.- (BOOL)isEqualToNumber:(NSNumber
*)aNumber
- (long)longValue
long
, converting
it as necessary.- (long long)longLongValue
long long
, converting
it as necessary.- (short)shortValue
short
, converting
it as necessary.- (NSString *)stringValue
- (unsigned char)unsignedCharValue
char
,
converting it as necessary.- (unsigned int)unsignedIntValue
int
, converting
it as necessary.- (unsigned long)unsignedLongValue
long
,
converting it as necessary.- (unsigned long long)unsignedLongLongValue
long long
,
converting it as necessary.- (unsigned short)unsignedShortValue
short
,
converting it as necessary.