Question

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?!

Was it helpful?

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.

OTHER TIPS

The BOOL literals rely on new language keywords that are defined as part of the iOS or Mac SDKs, so older SDKs (including iOS 5.1) do not have it, and therefore must use the boxing notation - i.e. @(YES).

As I mentioned in the comment above, my guess is that this is true of Object Subscripting as well.

(Answering my own question using source: http://jamesdempsey.net/2012/07/30/moving-to-new-objective-c-literals/)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top