是否可以使用NSArray,NSDictionary和NSNumber的“文字"?在Xcode 4.3中? (LLVM 4.0)

问题描述:

显然,新的Objective-C文字已经进入 c骨,从而揭开了所有NDA的阴影.

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

我的问题 ...我如何以上帝的名义 在Xcode⋜v4.3中使用这些构造(请参见下文).如果没有,那么我就一直在等待XCode 4.4/OSX 10.8/LLVM 4.0 trifecta,是否可以通过某些巧妙的类别以某种方式来伪装相同的功能?

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

我发现了一种非官方的方法……使用

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];

就是……

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

啊,简洁起见.