5

Apparently, the new Objective-C literals have landed into the clang trunk, and thus lifted the shadowy veil of any NDA's.

My question… HOW can I, in God's name, use these constructs (see below) in Xcode ⋜ v4.3. If not, and I'm stuck waiting for the XCode 4.4 / OSX 10.8 / LLVM 4.0 trifecta, could the same functionality be jerry-rigged somehow - via some clever categories, etc.?

(For all y'all that don't know… these new syntaxes mean that there will be the much-appreciated additional constructs for creating NSArray, NSDictionary, and NSNumber.)

jscs
  • 63,694
  • 13
  • 151
  • 195
Alex Gray
  • 16,007
  • 9
  • 96
  • 118
  • 2
    It's quite likely that the answer to [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) still largely applies. I think there are other similar questions linked from there, too. – jscs Apr 08 '12 at 19:58

2 Answers2

5

I found a non-official way to do this… Using the Lumumba Framework on github, there is a whole kit'n'caboodle of Syntactic sugar categories that had the following defines… which achieve the desired effect.

#define $(...)        ((NSString *)[NSString  stringWithFormat:__VA_ARGS__,nil])
#define $array(...)   ((NSArray *)[NSArray arrayWithObjects:__VA_ARGS__,nil])
#define $set(...)     ((NSSet *)[NSSet setWithObjects:__VA_ARGS__,nil])
#define $map(...)     ((NSDictionary *)[NSDictionary dictionaryWithObjectsAndKeys:__VA_ARGS__,nil])
#define $int(A)       [NSNumber numberWithInt:(A)]
#define $ints(...)    [NSArray arrayWithInts:__VA_ARGS__,NSNotFound]
#define $float(A)     [NSNumber numberWithFloat:(A)]
#define $doubles(...) [NSArray arrayWithDoubles:__VA_ARGS__,MAXFLOAT]
#define $words(...)   [[@#__VA_ARGS__ splitByComma] trimmedStrings]
#define $concat(A,...) { A = [A arrayByAddingObjectsFromArray:((NSArray *)[NSArray arrayWithObjects:__VA_ARGS__,nil])]; }

So, basically, instead of…

NSArray *anArray = [NSArray arrayWithObjects:
    object, @"aWord", [NSNumber numberWithInt:7], nil];

It's just…

NSArray *anArray = $array(object, @"aWord", $int(7));

Ahhh, brevity.

Alex Gray
  • 16,007
  • 9
  • 96
  • 118
2

Sorry, this is Xcode 4.4 only.

niklassaers
  • 8,480
  • 20
  • 99
  • 146
  • How would it depend on the version of the IDE? Xcode has nothing to do with this. It depends on the compiler version. –  Apr 08 '12 at 19:35
  • 1
    This is not a very useful answer; support for the syntax comes from the compiler, which _is_ available. The question is essentially how a different compiler than the default can be used in Xcode. – jscs Apr 08 '12 at 19:57
  • 1
    The way the question was asked, I assume XCode is not seen as the IDE, but the application you download. In the application, there comes llvm 3.0 (see Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang) – niklassaers Apr 13 '12 at 08:37