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

后端 未结 2 359
天命终不由人
天命终不由人 2021-01-13 05:41

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,

相关标签:
2条回答
  • 2021-01-13 05:49

    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.

    0 讨论(0)
  • 2021-01-13 06:11

    Sorry, this is Xcode 4.4 only.

    0 讨论(0)
提交回复
热议问题