Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 269
Joseph Humfrey Avatar asked Oct 06 '22 18:10

Joseph Humfrey


2 Answers

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.

like image 152
Cyrille Avatar answered Oct 13 '22 12:10

Cyrille


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/)

like image 35
Joseph Humfrey Avatar answered Oct 13 '22 10:10

Joseph Humfrey