Search code examples
objective-cxcode4objective-c-literals

Unboxed BOOLs in Objective-C literal syntax giving error


I was just experimenting with the new Objective-C literal syntax introduced as part of Xcode 4.4.

Dictionaries, integers, and arrays all work fine, but I've been having a problem getting BOOLs to work. e.g.:

NSDictionary *myDict = @{
    @"foo": @"bar",
    @"test": @YES
};

gives me "Unexpected type name 'BOOL': expected expression" on the line with the boolean.

However, @(YES), @1, @true all work fine.

This article: http://clang.llvm.org/docs/ObjectiveCLiterals.html suggests that @YES should work.

I've also tried it on its own line: NSNumber *myNum = @YES; and get the same error.

Bug?!


Solution

  • It's not a bug, it's because Apple currently does

    #define YES (BOOL)1
    #define NO  (BOOL)0
    

    instead of

    #define YES ((BOOL)1)
    #define NO  ((BOOL)0)
    

    which is fixed in the newest SDKs.