[swift-users] Foundation on Linux `CFBooleanGetTypeID`/`CFGetTypeID`

Jens Alfke jens at mooseyard.com
Tue May 24 15:03:36 CDT 2016


> On May 24, 2016, at 12:52 PM, Tony Parker <anthony.parker at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160524/56ac66e7/attachment.html>


More information about the swift-users mailing list