Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 312
Alex Gray Avatar asked Apr 08 '12 16:04

Alex Gray


People also ask

What is the difference between NSArray and nsmutablearray?

NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects. NSArray is “toll-free bridged” with its Core Foundation counterpart, CFArray. See Toll-Free Bridging for more information on toll-free bridging.

How do I access NSArray values in Swift?

For more information about object literals in Swift, see Literal Expression in The Swift Programming Language (Swift 4.1). In addition to the provided instance methods, such as object (at:), you can access NSArray values by their indexes using subscripting.

Can you override a method in an NSArray?

You may also choose to override, partially or fully, any other NSArray method for which you want to provide an alternative implementation. You might want to implement an initializer for your subclass that is suited to the backing store that the subclass is managing.

How do I make a subclass of an NSArray?

Remember that NSArray is the public interface for a class cluster and what this entails for your subclass. You must provide the storage for your subclass and implement the primitive methods that directly act on that storage. Before making a custom subclass of NSArray, investigate NSPointerArray and the corresponding Core Foundation type, CFArray.


2 Answers

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.

like image 127
Alex Gray Avatar answered Sep 19 '22 12:09

Alex Gray


Sorry, this is Xcode 4.4 only.

like image 39
niklassaers Avatar answered Sep 21 '22 12:09

niklassaers