It’s just days before WWDC and the rumors are flying about what Apple will introduce. Will it be iOS 6? new iPhone hardware? MacBook Pros? how about Mac Pros? One addition that isn’t actually a rumor but pretty much a confirmed fact is the addition of literals to Objective-C and the arrival of the Apple LLVM compiler.
The cat escaped the bag back in March when Apple checked in code to the open source clang trunk, over at LLVM.org, adding capabilities to make literals possible. Along with checking in the code came some very nice documentation that details what literals mean for developers. The Big Nerd Ranch Weblog followed the release by Apple with a slightly shorter explanation on literals.
If you have the time I would definitely recommend reading the LLVM.org article. The short version on literals is that Apple is bringing some of the comforts of Ruby to Objective-C by extending the responsibilities of the ‘@’ symbole. The slightly longer version is the Apple is adding syntactical sugar to NSNumber, NSArray, and NSDictionary.
For NSNumber Apple has simplified the process of creating them. You can now prepend a number with the ‘@’ symbol and presto, you’ve got an NSNumber object. There isn’t an equivalent way to get your number back out of NSNumber but this is definitely a good start.
For NSArray and NSDictionary Apple has added a bit of magic too. Now you can create an NSArray by putting your array inside a ‘@[]’ and an NSDictionary by putting your dictionary inside a ‘@{}’. Unlike NSNumber Apple added a nice way to get data back out of both NSArray and NSDictionary. The secret is ‘[]’. You put square brakets after either your NSArray or NSDictionary and then put your index, either number or object, inside the square brackets.
If you’re feeling particularly adventures you can download the current build of both llvm and clang from LLVM.org. Unfortunately at the moment Xcode doesn’t have these new features but wait a moment, well at least until Monday, and you’ll have Objective-C with Literals.