Inherits from: NSValue : NSObject
Conforms to: NSCoding
(NSValue)
NSCopying (NSValue)
NSObject (NSObject)
Declared in: Foundation/NSValue.h
Foundation/NSDecimalNumber.h
- typeValue | Returns the value of an NSNumber as a specific numeric type. |
- compare: | Compares two NSNumbers. |
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:
- - description
- - doubleValue
- - floatValue
- - intValue
- - longLongValue
- - longValue
- - shortValue
- - stringValue
- - unsignedCharValue
- - unsignedIntValue
- - unsignedLongLongValue
- - unsignedLongValue
- - unsignedShortValue
- Comparing NSNumbers
- - compare:
- - isEqualToNumber:
+ (NSNumber *)numberWithBool:(BOOL)value
+ (NSNumber *)numberWithChar:(char)value
char
.+ (NSNumber *)numberWithDouble:(double)value
double
.+ (NSNumber *)numberWithFloat:(float)value
float
.+ (NSNumber *)numberWithInt:(int)value
int
.+ (NSNumber *)numberWithLong:(long
int)value
long int
.+ (NSNumber *)numberWithLongLong:(long
long int)value
long long int
.+ (NSNumber *)numberWithShort:(short
int)value
short int
.+ (NSNumber *)numberWithUnsignedChar:(unsigned
char)value
unsigned char
.+ (NSNumber *)numberWithUnsignedInt:(unsigned
int)value
unsigned int
.+ (NSNumber *)numberWithUnsignedLong:(unsigned
long int)value
unsigned long int
.+ (NSNumber *)numberWithUnsignedLongLong:(unsigned
long long int)value
unsigned long long int
.+ (NSNumber *)numberWithUnsignedShort:(unsigned
short int)value
unsigned short int
.- (BOOL)boolValue
BOOL
,
converting it as necessary. 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. |
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 |
- (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
int)value
long int
.- (id)initWithLongLong:(long
long int)value
long long int
.- (id)initWithShort:(short
int)value
short int
.- (id)initWithUnsignedChar:(unsigned
char)value
unsigned char
.- (id)initWithUnsignedInt:(unsigned
int)value
unsigned int
.- (id)initWithUnsignedLong:(unsigned
long int)value
unsigned long int
.- (id)initWithUnsignedLongLong:(unsigned
long long int)value
unsigned long long int
.- (id)initWithUnsignedShort:(unsigned
short int)value
unsigned short int
.- (int)intValue
int
, converting
it as necessary.- (BOOL)isEqualToNumber:(NSNumber *)aNumber
- (long long int)longLongValue
long long int
, converting
it as necessary.- (long int)longValue
long int
, converting
it as necessary.- (short int)shortValue
short int
, converting
it as necessary.- (NSString *)stringValue
- (unsigned char)unsignedCharValue
char
,
converting it as necessary.- (unsigned int)unsignedIntValue
int
, converting
it as necessary.- (unsigned long long int)unsignedLongLongValue
long long int
,
converting it as necessary.- (unsigned long int)unsignedLongValue
long int
,
converting it as necessary.- (unsigned short int)unsignedShortValue
short int
,
converting it as necessary.