Boolean Values in a plist

  • I need to use the parameters obtained from a plist (via
    NSPropertyListSerialization). I can get the process to work quite
    well, except that I have to use in my code an undocumented class:
    NSCFBoolean.
    Determining that the returned object is of class NSCFBoolean is the
    only way I've been able to determine that I have a Boolean value. I
    can use the objCType method of
    NSNumber to distinguish between integers and floats, but a BOOL is an
    integer, which is IMHO a bad design decision. In some cases I can use
    a priori knowledge to ask for
    the boolValue, but in a generalized case I'm forced to use (the name
    of) an undocumented class (which was discovered by experimentation).
    I'd rather not have to go through a big learning curve to parse the
    raw XML, and this to me is a big hole in the design.

    Of course, I've likely missed something. I'd rather get flamed for
    that than for using an undocumented class. If so, please enlighten me.

    Regards,
    Dale Miller
    <dalelmiller...>
  • On May 23, 2010, at 8:23 PM, Dale Miller wrote:

    > I need to use the parameters obtained from a plist (via NSPropertyListSerialization). I can get the process to work quite well, except that I have to use in my code an undocumented class: NSCFBoolean.
    > Determining that the returned object is of class NSCFBoolean is the only way I've been able to determine that I have a Boolean value.

    I'm not absolutely sure, but I think that toll-free bridging would let you do this:

    CFGetTypeID( (CFTypeRef) value ) == CFBooleanGetTypeID()

    Experimentally, it works.  That is, CFGetTypeID( (CFTypeRef) [NSNumber numberWithFloat: 3.5] ) returns the same value as CFNumberGetTypeID(), whereas CFGetTypeID( (CFTypeRef) [NSNumber numberWithBool: YES] ) returns the same value as CFBooleanGetTypeID().
  • On May 23, 2010, at 10:23 PM, Dale Miller wrote:

    > I need to use the parameters obtained from a plist (via NSPropertyListSerialization). I can get the process to work quite well, except that I have to use in my code an undocumented class: NSCFBoolean.
    > Determining that the returned object is of class NSCFBoolean is the only way I've been able to determine that I have a Boolean value.

    Are you sure you need to distinguish booleans from other numbers?  There's a reason that all of them end up represented as NSNumber objects.

    In any case, you can drop down to Core Foundation to make the distinction, if necessary.

    CFGetTypeID(someObject) == CFBooleanGetTypeID()

    Regards,
    Ken
  • On May 23, 2010, at 20:23, Dale Miller wrote:

    > I need to use the parameters obtained from a plist (via NSPropertyListSerialization). I can get the process to work quite well, except that I have to use in my code an undocumented class: NSCFBoolean.
    > Determining that the returned object is of class NSCFBoolean is the only way I've been able to determine that I have a Boolean value. I can use the objCType method of
    > NSNumber to distinguish between integers and floats, but a BOOL is an integer, which is IMHO a bad design decision. In some cases I can use a priori knowledge to ask for
    > the boolValue, but in a generalized case I'm forced to use (the name of) an undocumented class (which was discovered by experimentation).
    > I'd rather not have to go through a big learning curve to parse the raw XML, and this to me is a big hole in the design.

    It's not clear what the issue is here.

    If you just need to know whether the value is YES or NO, you can simply ask for the numeric value (boolValue or one of the intValue siblings), and like everywhere else in Obj-C, non-zero means YES.

    If you need to know whether the value is  *typed* boolean in the serialization, you're kind of barking up the wrong tree, because the type used to encode the value is an implementation detail, not part of the API (AFAIK). 'objCType' doesn't seem reliable -- "The returned type does not necessarily match the method the receiver was created with," says the documentation -- even for distinguishing between integers and floating point.

    If, regardless, you must know the type of the object returned by the serialization, you just need to understand that all of the allowable types are toll-free bridged, so you can check for CFBoolean (which *is* documented, and of which NSCFBoolean is the underlying implementation) via CFGetTypeID.

    Admittedly, it is a little strange that CFBoolean is toll-free bridged but there isn't any NSBoolean* type for it to be bridged into.
  • On Sun, May 23, 2010 at 8:58 PM, Quincey Morris
    <quinceymorris...> wrote:
    > Admittedly, it is a little strange that CFBoolean is toll-free bridged but there isn't any NSBoolean* type for it to be bridged into.

    I don't believe this is documented anywhere, but CFBoolean is
    toll-free bridged to NSNumber. At least, just enough to get plists
    working. This is kind of implied by Ken's explanation above that
    NSNumber can be backed by either a CFNumber or CFBoolean.

    --Kyle Sluder
previous month may 2010 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today