> On May 24, 2016, at 12:52 PM, Tony Parker <anthony.par...@apple.com> wrote:
>
> One other possibility is using the objCType property on NSNumber’s superclass
> NSValue to check.
That doesn’t work, unfortunately, at least not with Apple’s Foundation.
NSNumbers initialized with booleans have objcType “c” because `BOOL` is just a
typedef for `char`. So the only way to tell a boolean apart from an 8-bit int
is to compare the object pointer against the singleton true and false objects.
Here’s a snippet of Obj-C code I use for this in my JSON encoder:
char ctype = self.objCType[0];
switch (ctype) {
case 'c': {
// The only way to tell whether an NSNumber with 'char' type is a
boolean is to
// compare it against the singleton kCFBoolean objects:
if (self == (id)kCFBooleanTrue)
return yajl_gen_bool(gen, true);
else if (self == (id)kCFBooleanFalse)
return yajl_gen_bool(gen, false);
else
return yajl_gen_integer(gen, self.longLongValue);
}
> I haven’t seen how much of this is implemented in corelibs-foundation yet.
I took a peek at the Swift NSNumber and NSValue implementations on Github, and
the objcType stuff doesn’t seem to be functional. It looks like objcType will
only have a value if the object was initialized as an NSValue with the type
code passed in, not if the typical NSNumber initializers were used.
—Jens
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users