5

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

jscs
  • 63,694
  • 13
  • 151
  • 195
Joseph Humfrey
  • 2,974
  • 2
  • 23
  • 34
  • [troll]xcode == bug[/troll]. But yes it seems to be a bug, quite strange... What is the version of clang behind Xcode 4.4 ? And are you sure you're using clang and not gcc ? – Jaffa Sep 03 '12 at 11:56
  • Compiler listed is "Apple LLVM compiler 4.0". Odd that it *mostly* works! Also tried "Object Subscripting" also mentioned on that page, which doesn't work, but perhaps that's because Apple needs to support it in their SDK by adding support to the [] operator for NSDictionary etc. (haven't tried with iOS 6 SDK) – Joseph Humfrey Sep 03 '12 at 12:08
  • 1
    possible duplicate of [Literal @YES not working in iOS 5 / Xcode 4.4](http://stackoverflow.com/questions/11685996/literal-yes-not-working-in-ios-5-xcode-4-4) – jscs Sep 11 '12 at 22:05

2 Answers2

3

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.

Cyrille
  • 25,014
  • 12
  • 67
  • 90
1

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

Joseph Humfrey
  • 2,974
  • 2
  • 23
  • 34