CFNumber Reference
| Derived from | |
| Framework | CoreFoundation/CoreFoundation.h |
| Companion guide | |
| Declared in | CFNumber.h |
Overview
CFNumber encapsulates C scalar (numeric) types. It provides functions for setting and accessing the value as any basic C type. It also provides a compare function to determine the ordering of two CFNumber objects. CFNumber objects are used to wrap numerical values for use in Core Foundation property lists and collections.
CFNumber objects are not intended as a replacement for C scalar values and should not be used in APIs or implementations where scalar values are more appropriate and efficient.
CFNumber is “toll-free bridged” with its Cocoa Foundation counterpart, NSNumber. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore, in a method where you see an NSNumber * parameter, you can pass in a CFNumberRef, and in a function where you see a CFNumberRef parameter, you can pass in an NSNumber instance. This fact also applies to concrete subclasses of NSNumber. See “Toll-Free Bridged Types” for more information on toll-free bridging.
Functions by Task
Creating a Number
Getting Information About Numbers
Comparing Numbers
Getting the CFNumber Type ID
Functions
CFNumberCompare
Compares two CFNumber objects and returns a comparison result.
CFComparisonResult CFNumberCompare ( CFNumberRef number, CFNumberRef otherNumber, void *context );
Parameters
- number
The first CFNumber object to compare.
- otherNumber
The second CFNumber object to compare.
- context
Pass
NULL.
Return Value
A CFComparisonResult constant that indicates whether number is equal to, less than, or greater than otherNumber.
Discussion
When comparing two CFNumber objects using this function, one or both objects can represent a special-case number such as signed 0, signed infinity, or NaN.
The following rules apply:
Negative 0 compares less than positive 0.
Positive infinity compares greater than everything except itself, to which it compares equal.
Negative infinity compares less than everything except itself, to which it compares equal.
If both numbers are NaN, then they compare equal.
If only one of the numbers is NaN, then the NaN compares greater than the other number if it is negative, and smaller than the other number if it is positive.
Availability
- Available in iOS 2.0 and later.
Declared In
CFNumber.hCFNumberCreate
Creates a CFNumber object using a specified value.
CFNumberRef CFNumberCreate ( CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr );
Parameters
- allocator
The allocator to use to allocate memory for the new object. Pass
NULLorkCFAllocatorDefaultto use the default allocator.- theType
A constant that specifies the data type of the value to convert. See Number Types for a list of possible values.
- valuePtr
A pointer to the value for the returned number object.
Return Value
A new number with the value specified by valuePtr. Ownership follows the Create Rule.
Discussion
The theType parameter is not necessarily preserved when creating a new CFNumber object. The CFNumber object will be created using whatever internal storage type the creation function deems appropriate. Use the function CFNumberGetType to find out what type the CFNumber object used to store your value.
Availability
- Available in iOS 2.0 and later.
Declared In
CFNumber.hCFNumberGetByteSize
Returns the number of bytes used by a CFNumber object to store its value.
CFIndex CFNumberGetByteSize ( CFNumberRef number );
Parameters
- number
The CFNumber object to examine.
Return Value
The size in bytes of the value contained in number.
Discussion
Because a CFNumber object might store a value using a type different from that of the original value with which it was created, this function may return a size different from the size of the original value's type.
Availability
- Available in iOS 2.0 and later.
Declared In
CFNumber.hCFNumberGetType
Returns the type used by a CFNumber object to store its value.
CFNumberType CFNumberGetType ( CFNumberRef number );
Parameters
- number
The CFNumber object to examine.
Return Value
A constant that indicates the data type of the value contained in number. See Number Types for a list of possible values.
Discussion
The type specified in the call to CFNumberCreate is not necessarily preserved when a new CFNumber object is created—it uses whatever internal storage type the creation function deems appropriate.
Availability
- Available in iOS 2.0 and later.
Declared In
CFNumber.hCFNumberGetTypeID
Returns the type identifier for the CFNumber opaque type.
CFTypeID CFNumberGetTypeID ( void );
Return Value
The type identifier for the CFNumber opaque type.
Availability
- Available in iOS 2.0 and later.
Declared In
CFNumber.hCFNumberGetValue
Obtains the value of a CFNumber object cast to a specified type.
Boolean CFNumberGetValue ( CFNumberRef number, CFNumberType theType, void *valuePtr );
Parameters
- number
The CFNumber object to examine.
- theType
A constant that specifies the data type to return. See Number Types for a list of possible values.
- valuePtr
On return, contains the value of number.
Return Value
true if the operation was successful, otherwise false.
Discussion
If the argument type differs from the return type, and the conversion is lossy or the return value is out of range, then this function passes back an approximate value in valuePtr and returns false.
Availability
- Available in iOS 2.0 and later.
Declared In
CFNumber.hCFNumberIsFloatType
Determines whether a CFNumber object contains a value stored as one of the defined floating point types.
Boolean CFNumberIsFloatType ( CFNumberRef number );
Parameters
- number
The CFNumber object to examine.
Return Value
true if number's value is one of the defined floating point types, otherwise false. The valid floating point types are listed in Number Types.
Availability
- Available in iOS 2.0 and later.
Declared In
CFNumber.hData Types
CFNumberRef
A reference to a CFNumber object.
typedef const struct __CFNumber *CFNumberRef;
Availability
- Available in iOS 2.0 and later.
Declared In
CFNumber.hConstants
Number Types
Flags used by CFNumber to indicate the data type of a value.
enum CFNumberType {
kCFNumberSInt8Type = 1,
kCFNumberSInt16Type = 2,
kCFNumberSInt32Type = 3,
kCFNumberSInt64Type = 4,
kCFNumberFloat32Type = 5,
kCFNumberFloat64Type = 6,
kCFNumberCharType = 7,
kCFNumberShortType = 8,
kCFNumberIntType = 9,
kCFNumberLongType = 10,
kCFNumberLongLongType = 11,
kCFNumberFloatType = 12,
kCFNumberDoubleType = 13,
kCFNumberCFIndexType = 14,
kCFNumberNSIntegerType = 15,
kCFNumberCGFloatType = 16,
kCFNumberMaxType = 16
};
typedef enum CFNumberType CFNumberType;
Constants
kCFNumberSInt8TypeEight-bit, signed integer. The
SInt8data type is defined inMacTypes.h.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberSInt16TypeSixteen-bit, signed integer. The
SInt16data type is defined inMacTypes.h.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberSInt32TypeThirty-two-bit, signed integer. The
SInt32data type is defined inMacTypes.h.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberSInt64TypeSixty-four-bit, signed integer. The
SInt64data type is defined inMacTypes.h.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberFloat32TypeThirty-two-bit real. The
Float32data type is defined inMacTypes.h.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberFloat64TypeSixty-four-bit real. The
Float64data type is defined inMacTypes.hand conforms to the 64-bit IEEE 754 standard.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberCharTypeBasic C
chartype.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberShortTypeBasic C
shorttype.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberIntTypeBasic C
inttype.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberLongTypeBasic C
longtype.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberLongLongTypeBasic C
long longtype.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberFloatTypeBasic C
floattype.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberDoubleTypeBasic C
doubletype.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberCFIndexTypeCFIndex value.
Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberNSIntegerTypeNSIntegervalue.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberCGFloatTypeCGFloatvalue.Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberMaxTypeSame as
kCFNumberCGFloatType.Note that on OS X v10.4,
kCFNumberMaxTypewas the same askCFNumberCFIndexType.Available in iOS 2.0 and later.
Declared in
CFNumber.h.
Discussion
The type specified in the call to CFNumberCreate is not necessarily preserved when creating a new CFNumber object. A CFNumber object uses whatever internal storage type the creation function deems appropriate. Use the CFNumberGetType function to find out what type the CFNumber object used to store your value.
Predefined Values
CFNumber provides some predefined number values.
const CFNumberRef kCFNumberNaN; const CFNumberRef kCFNumberNegativeInfinity; const CFNumberRef kCFNumberPositiveInfinity;
Constants
kCFNumberNaN“Not a Number.” This value is often the result of an invalid operation, such as the square-root of a negative number.
Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberNegativeInfinityDesignates a negative infinity value.
Available in iOS 2.0 and later.
Declared in
CFNumber.h.kCFNumberPositiveInfinityDesignates a positive infinity value.
Available in iOS 2.0 and later.
Declared in
CFNumber.h.
© 2003, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-12-01)