On Aug 19, 2012, at 15:12 , Alexander von Below < email@hidden> wrote: I am not sure if I am answering the question, but the safest way I know of to check if the NSNumber object is an CFBooleanRef is this:
CFGetTypeID(numberObject) == CFBooleanGetTypeID()
(and as you said, it is hard to imagine that being something else than kCFBooleanTrue or kCFBooleanFalse
This is instructive:
- (void) logNumber: (NSNumber*) number description: (NSString*) description { NSLog (@"%p %s %lu %@ %@ %@", number, number.objCType, CFNumberGetType ((__bridge CFNumberRef) number), NSStringFromClass ([number class]), NSStringFromClass ([number superclass]), description); }
const char _one_ = 1; [self logNumber: (__bridge NSNumber*) CFNumberCreate (NULL, kCFNumberCharType, &one) description: @"CFNumberCreate (NULL, kCFNumberCharType, &one)"]; [self logNumber: [NSNumber numberWithUnsignedChar: 1] description: @"[NSNumber numberWithUnsignedChar: 1]"]; [self logNumber: [NSNumber numberWithChar: 1] description: @"[NSNumber numberWithChar: 1]"]; [self logNumber: [NSNumber numberWithBool: YES] description: @"[NSNumber numberWithBool: YES]"]; [self logNumber: (__bridge NSNumber*) kCFBooleanTrue description: @"kCFBooleanTrue"];
The output is:
0x107 c 1 __NSCFNumber NSNumber CFNumberCreate (NULL, kCFNumberCharType, &one) 0x147 s 2 __NSCFNumber NSNumber [NSNumber numberWithUnsignedChar: 1] 0x107 c 1 __NSCFNumber NSNumber [NSNumber numberWithChar: 1] 0x7fff78932ab0 c 7 __NSCFBoolean NSNumber [NSNumber numberWithBool: YES] 0x7fff78932ab0 c 7 __NSCFBoolean NSNumber kCFBooleanTrue
This has some interesting take-aways:
-- Booleans are a different Obj-C class within the NSNumber cluster.
-- CFBoolean behaves like a subtype of CFNumber.
-- CFNumberType 7 seems to be reserved for CFBooleans. Creating a CFNumber with type 7 actually creates a type 1 (signed char). Creating a NSNumber with a signed or unsigned character actually creates type 1 or type 2 (signed short).
-- "objCType" isn't a reliable guide to what the object represents, nor how it was created.
However, it's worth saying again that though this may be what *happens*, this is not what is *documented* to happen. I can see nothing documented to produce "true"/"false" in a JSON serialization, because '[NSNumber numberWithBool: YES]' doesn't come with a guarantee. Nor does '(__bridge NSNumber*) kCFBooleanTrue', for that matter, since CFBoolean isn't even documented as toll-free bridged AFAICT.
|