Search code examples
iosobjective-cbooleankvc

BOOL property KVC: is this behavior a bug?


It appears that valueForKey: with a BOOL @property name for the key returns different types (and, consequently, leads to different JSON serializations) on 64-bit and 32-bit iOS systems:

On 32-bit (iPhone 4s simulator):

8 * sizeof(void*) = 32
boolProperty attributes: Tc,N,V_boolProperty
obj.boolProperty = YES
[obj valueForKey:@"boolProperty"]: 1
[[obj valueForKey:@"boolProperty"] class]: __NSCFNumber
JSON serialization of @{ @"boolProperty": [obj valueForKey:@"boolProperty"] }: {"boolProperty":1}
obj.boolProperty = NO
[obj valueForKey:@"boolProperty"]: 0
[[obj valueForKey:@"boolProperty"] class]: __NSCFNumber
JSON serialization of @{ @"boolProperty": [obj valueForKey:@"boolProperty"] }: {"boolProperty":0}

On 64-bit (iPhone 6 simulator):

8 * sizeof(void*) = 64
boolProperty attributes: TB,N,V_boolProperty
obj.boolProperty = YES
[obj valueForKey:@"boolProperty"]: 1
[[obj valueForKey:@"boolProperty"] class]: __NSCFBoolean
JSON serialization of @{ @"boolProperty": [obj valueForKey:@"boolProperty"] }: {"boolProperty":true}
obj.boolProperty = NO
[obj valueForKey:@"boolProperty"]: 0
[[obj valueForKey:@"boolProperty"] class]: __NSCFBoolean
JSON serialization of @{ @"boolProperty": [obj valueForKey:@"boolProperty"] }: {"boolProperty":false}

The code:

@interface TSTObject : NSObject

@property (nonatomic, assign) BOOL boolProperty;

@end

[…]

NSLog(@"8 * sizeof(void*) = %lu", 8 * sizeof(void*));

objc_property_t boolProp = class_getProperty([TSTObject class], "boolProperty");
NSLog(@"boolProperty attributes: %s", property_getAttributes(boolProp));

void(^testBool)(BOOL) = ^(BOOL boolValue) {
    TSTObject *obj = [[TSTObject alloc] init];
    id vfk;
    obj.boolProperty = boolValue;
    vfk = [obj valueForKey:@"boolProperty"];
    NSLog(@"obj.boolProperty = %@", boolValue ? @"YES" : @"NO");
    NSLog(@"[obj valueForKey:@\"boolProperty\"]: %@", vfk);
    NSLog(@"[[obj valueForKey:@\"boolProperty\"] class]: %@", [vfk class]);
    NSLog(@"JSON serialization of @{ @\"boolProperty\": [obj valueForKey:@\"boolProperty\"] }: %@",
          [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:@{@"boolProperty": vfk}
                                                                         options:0
                                                                           error:NULL]
                                encoding:NSUTF8StringEncoding]);
};

testBool(YES);
testBool(NO);

This is all with Xcode 7.2, iOS 9.2 simulators.

It seems likely that the underlying cause is, to quote this answer:

Remark: On the 64-bit iOS platform (but not on 64-bit OS X), BOOL is defined as the C99 _Bool, which is a "proper" boolean type and can take only the value 0 or 1.

Is that the cause?

Is this a bug or intended behavior?


Solution

  • Yes, that's the cause.

    Yes, that is intended behavior. (Well, the way it serializes to JSON on 32-bit isn't particularly "intended" but it is expected. The fact that 64-bit uses a proper Bool type is intended.)

    The JSON serializer has no way to tell the difference between a one-byte signed integer and a boolean on 32-bit, because they are in fact the same thing.