1

In other words, use literals but target for iOS 5.

I am aware of this but that post is not conclusive.

Community
  • 1
  • 1
james_womack
  • 10,028
  • 6
  • 55
  • 74
  • There's at least two posts that this is a duplicate of: the one you've linked and http://stackoverflow.com/questions/11361180/using-new-objective-c-literals?lq=1. More importantly, have you tried the solution at [Is there any way to use LLVM 3 in Xcode 4.1](http://stackoverflow.com/questions/7361824/is-there-any-way-to-use-llvm-3-in-xcode-4-1)? – jscs Jul 10 '12 at 04:33
  • If you have access to the Mac Developer Program you can download Xcode 4.4 GM today. Otherwise, wait a few days and you'll be able to use literals for iOS5 code. – Alan Zeino Jul 10 '12 at 04:39
  • Is there any way to use LLVM 3 in Xcode 4.1? has been tried, it makes no difference in what compiler is used in Xcode 4.3. – james_womack Jul 10 '12 at 05:35

1 Answers1

22

You can switch the compiler out, but it doesn't work as you might think (I tried it just now). There are a few requirements of the compiling SDK for using this new syntax (i.e. it will work on previous iOS versions, but you need to compile it with the iOS 6.0 SDK). I don't think I am allowed to discuss them here at the moment, but they are covered in detail in the "Modern Objective-C" WWDC 2012 video.

Basically, you will have the syntax, but the libraries will not know how to respond to it.

There is an alternate theory though...that I just didn't do it right ^^;

In the end though, you don't have to use 4.3 to deploy to 5.x apps. So my suggestion is just upgrade, there is really no downside to it (unless you are deploying an app soon, but in that case it would just be a hassle to convert everything since I assume you are almost done).

EDIT (2 months later now iOS 6 is public). You can definitely use categories to get around the SDK requirements of the new objective-c literal syntax. Add categories on NSArray and NSDictionary for - (id)objectAtIndexedSubscript:(NSUInteger)idx and - (id)objectForKeyedSubscript:(id)key respectively that just return objectAtIndex: and objectForKey: (that is what they do anyway in iOS 6). Also for NSMutableArray and NSMutableDictionary add category methods for - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)index and - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)index respectively that simply add the object if it doesn't already exist, and replace it if it does.

One catch: The definitions of YES and NO changed in iOS 6 to accommodate boolean literals for NSNumber. So you need to do the following:

#if __has_feature(objc_bool)
#undef YES //Before it was (BOOL)1
#undef NO //Before it was (BOOL)0

#define YES __objc_yes
#define NO __objc_no
#endif

The reason is that in the new syntax already has a definition for the @( ... ) syntax (boxed expressions) and the old definition would resolve to @(BOOL)1 which is an error.

borrrden
  • 33,256
  • 8
  • 74
  • 109
  • The videos are publicly available; there doesn't seem to be any reason not to discuss their contents. The new syntax itself is public, too: http://clang.llvm.org/docs/ObjectiveCLiterals.html, so you can probably just go ahead and say "Current versions of Foundation objects don't implement `objectAtIndexedSubscript:` and other methods". – jscs Jul 10 '12 at 06:39
  • @JoshCaswell Don't the videos require you to log in to get them? – borrrden Jul 10 '12 at 06:40
  • Yes, but a free account is sufficient, which is no real obstacle. It's your "signature" on your copy of the NDA, however; I'm not trying to push anything. Thanks for trying it out, BTW! – jscs Jul 10 '12 at 06:48
  • @JoshCaswell Ah, I didn't know that (my employer pays for the account so I've never registered for a free one). Thanks for the tip. – borrrden Jul 10 '12 at 07:41
  • @JoshCaswell Is adding `objectAtIndexedSubscript` in some categories an option? So far, I've created a couple macros to alias to either literals or bracket notation depending on `#if __has_feature(objc_boxed_expressions)` – james_womack Jul 10 '12 at 13:44
  • @Cirrostratus: There are other methods involved, too; see the Clang doc for details. I can't think of a reason off the top of my head why that shouldn't work, but you'll certainly want to remove your implementations when compiling with the newer SDK. – jscs Jul 10 '12 at 16:46
  • @Cirrostratus To come back to this, it is completely possible (and what I did until iOS 6 was released). The only catch is that you need to also redefine `YES` and `NO` because they changed in iOS 6. You can see the details in the clang link above from Josh. – borrrden Sep 24 '12 at 05:05