So you’ve no doubt taken a peek at NDA’d stuff and got the impression there’s some changes to Objective-C coming down the pike, and perhaps you noticed that there’s official documentation out now,
Three new features were introduced into clang at the same time: NSNumber Literals provide a syntax for creating NSNumber from scalar literal expressions; Collection Literals provide a short-hand for creating arrays and dictionaries; Object Subscripting provides a way to use subscripting with Objective-C objects. Users of Apple compiler releases can use these features starting with the Apple LLVM Compiler 4.0. Users of open-source LLVM.org compiler releases can use these features starting with clang v3.1.
These language additions simplify common Objective-C programming patterns, make programs more concise, and improve the safety of container creation…
But you might not have noticed these dissections over at the Big Nerd Ranch:
The second one is particularly worth reading; goes into some not immediately obvious consequences of being able to use the new syntax on your own classes, like
A Negative-Indexed Array
Being able to index arrays from the end can be convenient. In languages like Python you can access the last element of an array with array[-1], the penultimate element with array[-2], the antepenultimate element with array[-3] (thanks to the Pittsburgh CocoaHeads for introducing me to this term) and so on.
Here is a mutable collection that supports regular and negative indexing. Under the hood it uses an NSMutableArray to actually hold the objects, but you could use any data structure you wanted to…
Or how about
A Quasi-Key-Path Dictionary
Time for another simple sample collection, this time a collection with dictionary-like semantics. A convenient data structure is nested dictionaries holding a hierarchy of data, digging deeper from one dictionary to another to access the final value you want. You can do this with NSDictionaries by using Key-Value Coding and appropriately constructed key paths. This collection, called BNRDiggyDict, will let you construct and dig into nested dictionaries using a unix-style path, a string that @”looks/like/this”. This particular path will look for a dictionary under the key “looks“, then look up “like” inside of that dictionary to get another one, then look up “this” inside of that dictionary to get the final values…
There’s some pretty seriously powerful abstraction possibilities here, indeed. On the other hand, there’s some pretty serious possibilities of turning your code into the kind of mishmash written by the C++ nouvelle gourou who just discovered operator overloading, too…
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)