Is it possible to use NSArray, NSDictionary, and NSNumber “literals” in Xcode 4.3? (LLVM 4.0)

怎甘沉沦 提交于 2019-12-01 04:40:22

问题


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


回答1:


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.




回答2:


Sorry, this is Xcode 4.4 only.



来源:https://stackoverflow.com/questions/10064311/is-it-possible-to-use-nsarray-nsdictionary-and-nsnumber-literals-in-xcode-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!