| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in Mac OS X v10.0 and later. |
| Declared in | NSDecimalNumber.h NSValue.h |
| Companion guides | |
| Related sample code |
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.
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”.
+ numberWithBool:
+ numberWithChar:
+ numberWithDouble:
+ numberWithFloat:
+ numberWithInt:
+ numberWithInteger:
+ numberWithLong:
+ numberWithLongLong:
+ numberWithShort:
+ numberWithUnsignedChar:
+ numberWithUnsignedInt:
+ numberWithUnsignedInteger:
+ numberWithUnsignedLong:
+ numberWithUnsignedLongLong:
+ numberWithUnsignedShort:
– initWithBool:
– initWithChar:
– initWithDouble:
– initWithFloat:
– initWithInt:
– initWithInteger:
– initWithLong:
– initWithLongLong:
– initWithShort:
– initWithUnsignedChar:
– initWithUnsignedInt:
– initWithUnsignedInteger:
– initWithUnsignedLong:
– initWithUnsignedLongLong:
– initWithUnsignedShort:
– boolValue
– charValue
– decimalValue
– doubleValue
– floatValue
– intValue
– integerValue
– longLongValue
– longValue
– shortValue
– unsignedCharValue
– unsignedIntegerValue
– unsignedIntValue
– unsignedLongLongValue
– unsignedLongValue
– unsignedShortValue
Creates and returns an NSNumber object containing a given value, treating it as a BOOL.
+ (NSNumber *)numberWithBool:(BOOL)value
The value for the new number.
An NSNumber object containing value, treating it as a BOOL.
NSValue.h
Creates and returns an NSNumber object containing a given value, treating it as a signed char.
+ (NSNumber *)numberWithChar:(char)value
The value for the new number.
An NSNumber object containing value, treating it as a signed char.
NSValue.h
Creates and returns an NSNumber object containing a given value, treating it as a double.
+ (NSNumber *)numberWithDouble:(double)value
The value for the new number.
An NSNumber object containing value, treating it as a double.
NSValue.h
Creates and returns an NSNumber object containing a given value, treating it as a float.
+ (NSNumber *)numberWithFloat:(float)value
The value for the new number.
An NSNumber object containing value, treating it as a float.
NSValue.h
Creates and returns an NSNumber object containing a given value, treating it as a signed int.
+ (NSNumber *)numberWithInt:(int)value
The value for the new number.
An NSNumber object containing value, treating it as a signed int.
NSValue.hCreates and returns an NSNumber object containing a given value, treating it as an NSInteger.
+ (NSNumber *)numberWithInteger:(NSInteger)value
The value for the new number.
An NSNumber object containing value, treating it as an NSInteger.
NSValue.h
Creates and returns an NSNumber object containing a given value, treating it as a signed long.
+ (NSNumber *)numberWithLong:(long)value
The value for the new number.
An NSNumber object containing value, treating it as a signed long.
NSValue.h
Creates and returns an NSNumber object containing a given value, treating it as a signed long long.
+ (NSNumber *)numberWithLongLong:(long long)value
The value for the new number.
An NSNumber object containing value, treating it as a signed long long.
NSValue.h
Creates and returns an NSNumber object containing value, treating it as a signed short.
+ (NSNumber *)numberWithShort:(short)value
The value for the new number.
An NSNumber object containing value, treating it as a signed short.
NSValue.hCreates and returns an NSNumber object containing a given value, treating it as an unsigned char.
+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value
The value for the new number.
An NSNumber object containing value, treating it as an unsigned char.
NSValue.hCreates and returns an NSNumber object containing a given value, treating it as an unsigned int.
+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value
The value for the new number.
An NSNumber object containing value, treating it as an unsigned int.
NSValue.hCreates and returns an NSNumber object containing a given value, treating it as an NSUInteger.
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value
The value for the new number.
An NSNumber object containing value, treating it as an NSUInteger.
NSValue.hCreates and returns an NSNumber object containing a given value, treating it as an unsigned long.
+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value
The value for the new number.
An NSNumber object containing value, treating it as an unsigned long.
NSValue.hCreates and returns an NSNumber object containing a given value, treating it as an unsigned long long.
+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value
The value for the new number.
An NSNumber object containing value, treating it as an unsigned long long.
NSValue.hCreates and returns an NSNumber object containing a given value, treating it as an unsigned short.
+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value
The value for the new number.
An NSNumber object containing value, treating it as an unsigned short.
NSValue.hReturns the receiver’s value as a BOOL.
- (BOOL)boolValue
The receiver’s value as a BOOL, converting it as necessary.
Prior to Mac OS X v10.3, the value returned isn’t guaranteed to be one of YES or NO. A 0 value always means NO or false, but any nonzero value should be interpreted as YES or true.
NSValue.hReturns the receiver’s value as a char.
- (char)charValue
The receiver’s value as a char, converting it as necessary.
NSValue.hReturns an NSComparisonResult value that indicates whether the receiver is greater than, equal to, or less than a given number.
- (NSComparisonResult)compare:(NSNumber *)aNumber
The number with which to compare the receiver.
This value must not be nil. If the value is nil, the behavior is undefined and may change in future versions of Mac OS X.
NSOrderedAscending if the value of aNumber is greater than the receiver’s, NSOrderedSame if they’re equal, and NSOrderedDescending if the value of aNumber is less than the receiver’s.
The compare: method follows the standard C rules for type conversion. For example, if you compare an NSNumber object that has an integer value with an NSNumber object that has a floating point value, the integer value is converted to a floating-point value for comparison.
NSValue.h
Returns the receiver’s value, expressed as an NSDecimal structure.
- (NSDecimal)decimalValue
The receiver’s value, expressed as an NSDecimal structure. The value returned isn’t guaranteed to be exact for float and double values.
NSDecimalNumber.h
Returns a string that represents the contents of the receiver for a given locale.
- (NSString *)descriptionWithLocale:(id)aLocale
An object containing locale information with which to format the description. Use nil if you don’t want the description formatted.
A string that represents the contents of the receiver formatted using the locale information in locale.
For example, if you have an NSNumber object that has the integer value 522, sending it the descriptionWithLocale: message returns the string “522”.
To obtain the string representation, this method invokes NSString’s initWithFormat:locale: method, supplying the format based on the type the NSNumber object was created with:
Data Type |
Format Specification |
|---|---|
char |
%i |
double |
%0.16g |
float |
%0.7g |
int |
%i |
long |
%li |
long long |
%lli |
short |
%hi |
unsigned char |
%u |
unsigned int |
%u |
unsigned long |
%lu |
unsigned long long |
%llu |
unsigned short |
%hu |
NSValue.h
Returns the receiver’s value as a double.
- (double)doubleValue
The receiver’s value as a double, converting it as necessary.
NSValue.h
Returns the receiver’s value as a float.
- (float)floatValue
The receiver’s value as a float, converting it as necessary.
NSValue.h
Returns an NSNumber object initialized to contain a given value, treated as a BOOL.
- (id)initWithBool:(BOOL)value
The value for the new number.
An NSNumber object containing value, treating it as a BOOL.
NSValue.h
Returns an NSNumber object initialized to contain a given value, treated as a signed char.
- (id)initWithChar:(char)value
The value for the new number.
An NSNumber object containing value, treating it as a signed char.
NSValue.h
Returns an NSNumber object initialized to contain value, treated as a double.
- (id)initWithDouble:(double)value
The value for the new number.
An NSNumber object containing value, treating it as a double.
NSValue.h
Returns an NSNumber object initialized to contain a given value, treated as a float.
- (id)initWithFloat:(float)value
The value for the new number.
An NSNumber object containing value, treating it as a float.
NSValue.h
Returns an NSNumber object initialized to contain a given value, treated as a signed int.
- (id)initWithInt:(int)value
The value for the new number.
An NSNumber object containing value, treating it as a signed int.
NSValue.hReturns an NSNumber object initialized to contain a given value, treated as an NSInteger.
- (id)initWithInteger:(NSInteger)value
The value for the new number.
An NSNumber object containing value, treating it as an NSInteger.
NSValue.h
Returns an NSNumber object initialized to contain a given value, treated as a signed long.
- (id)initWithLong:(long)value
The value for the new number.
An NSNumber object containing value, treating it as a signed long.
NSValue.h
Returns an NSNumber object initialized to contain value, treated as a signed long long.
- (id)initWithLongLong:(long long)value
The value for the new number.
An NSNumber object containing value, treating it as a signed long long.
NSValue.h
Returns an NSNumber object initialized to contain a given value, treated as a signed short.
- (id)initWithShort:(short)value
The value for the new number.
An NSNumber object containing value, treating it as a signed short.
NSValue.h
Returns an NSNumber object initialized to contain a given value, treated as an unsigned char.
- (id)initWithUnsignedChar:(unsigned char)value
The value for the new number.
An NSNumber object containing value, treating it as an unsigned char.
NSValue.h
Returns an NSNumber object initialized to contain a given value, treated as an unsigned int.
- (id)initWithUnsignedInt:(unsigned int)value
The value for the new number.
An NSNumber object containing value, treating it as an unsigned int.
NSValue.hReturns an NSNumber object initialized to contain a given value, treated as an NSUInteger.
- (id)initWithUnsignedInteger:(NSUInteger)value
The value for the new number.
An NSNumber object containing value, treating it as an NSUInteger.
NSValue.h
Returns an NSNumber object initialized to contain a given value, treated as an unsigned long.
- (id)initWithUnsignedLong:(unsigned long)value
The value for the new number.
An NSNumber object containing value, treating it as an unsigned long.
NSValue.hReturns an NSNumber object initialized to contain a given value, treated as an unsigned long long.
- (id)initWithUnsignedLongLong:(unsigned long long)value
The value for the new number.
An NSNumber object containing value, treating it as an unsigned long long.
NSValue.h
Returns an NSNumber object initialized to contain a given value, treated as an unsigned short.
- (id)initWithUnsignedShort:(unsigned short)value
The value for the new number.
An NSNumber object containing value, treating it as an unsigned short.
NSValue.hReturns the receiver’s value as an NSInteger.
- (NSInteger)integerValue
The receiver’s value as an NSInteger, converting it as necessary.
NSValue.hReturns the receiver’s value as an int.
- (int)intValue
The receiver’s value as an int, converting it as necessary.
NSValue.h
Returns a Boolean value that indicates whether the receiver and a given number are equal.
- (BOOL)isEqualToNumber:(NSNumber *)aNumber
The number with which to compare the receiver.
YES if the receiver and aNumber are equal, otherwise NOr
Two NSNumber objects are considered equal if they have the same id values or if they have equivalent values (as determined by the compare: method).
This method is more efficient than compare: if you know the two objects are numbers.
NSValue.h
Returns the receiver’s value as a long long.
- (long long)longLongValue
The receiver’s value as a long long, converting it as necessary.
NSValue.hReturns the receiver’s value as a long.
- (long)longValue
The receiver’s value as a long, converting it as necessary.
NSValue.hReturns a C string containing the Objective-C type of the data contained in the receiver.
- (const char *)objCType
A C string containing the Objective-C type of the data contained in the receiver, as encoded by the @encode() compiler directive.
The returned type does not necessarily match the method the receiver was created with.
Returns the receiver’s value as a short.
- (short)shortValue
The receiver’s value as a short, converting it as necessary.
NSValue.hReturns the receiver’s value as a human-readable string.
- (NSString *)stringValue
The receiver’s value as a human-readable string, created by invoking descriptionWithLocale: where locale is nil.
NSValue.h
Returns the receiver’s value as an unsigned char.
- (unsigned char)unsignedCharValue
The receiver’s value as an unsigned char, converting it as necessary.
NSValue.hReturns the receiver’s value as an NSUInteger.
- (NSUInteger)unsignedIntegerValue
The receiver’s value as an NSUInteger, converting it as necessary.
NSValue.h
Returns the receiver’s value as an unsigned int.
- (unsigned int)unsignedIntValue
The receiver’s value as an unsigned int, converting it as necessary.
NSValue.h
Returns the receiver’s value as an unsigned long long.
- (unsigned long long)unsignedLongLongValue
The receiver’s value as an unsigned long long, converting it as necessary.
NSValue.h
Returns the receiver’s value as an unsigned long.
- (unsigned long)unsignedLongValue
The receiver’s value as an unsigned long, converting it as necessary.
NSValue.h
Returns the receiver’s value as an unsigned short.
- (unsigned short)unsignedShortValue
The receiver’s value as an unsigned short, converting it as necessary.
NSValue.h
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-02-08)