I was going through the release notes for Xcode 4.4 and noticed this:
LLVM 4.0 Compiler
Xcode now includes the Apple LLVM Compiler version 4.0, including the following newObjective-C language features: [...]
- Objective-C literals: create literals for NSArray, NSDictionary, and NSNumber, just the same as the literals for NSString
I'm intrigued about this feature. It's not entirely clear to me just how literals for NSString
work and how one could use them on NSArray
, NSDictionary
, and NSNumber
.
What are the details?
The Objective-C compiler has hardcoded knowledge of the memory layout of instances of the
NSConstantString
class, aka the__CFConstantString
class. Check out theRewriteObjCStringLiteral
function inlib/Rewrite/RewriteModernObjC.cpp
in the clang source code. The compiler simply emits data that matches the layout of instances of theNSConstantString
class.There are a couple of possibilities for literal
NSArray
andNSDictionary
instances. They could do something like what they did for literal strings - hardcode the instance layout (for a special subclass) in the compiler and emit data in that layout. Or they could have the compiler emit code that simply creates an instance at runtime.From “Objective-C Literals”
1)
NSNumber
,NSDictionary
andNSArray
literals are available in Xcode 4.4.2)
NSDictionary
andNSArray
subscripting need "Xcode 4.4 and OS X 10.8 or later SDK" or "Xcode 4.5 and iOS 6 or later SDK"Looks to me like the subscripting needs runtime support and hence won't work before iOS6.
Copied verbatim from http://cocoaheads.tumblr.com/post/17757846453/objective-c-literals-for-nsdictionary-nsarray-and:
Objective-C literals: one can now create literals for NSArray, NSDictionary, and NSNumber (just like one can create literals for NSString)
NSArray Literals
Previously:
Now:
NSDictionary Literals
Previously:
Now:
NSNumber Literals
Previously:
Now:
[Edit]
zxoq at http://news.ycombinator.com/item?id=3672744 has added more interesting new subscripting. (Added with literals):
[Edit 2]
The new ObjC literals were discussed in multiple WWDC 2012 sessions. I intentionally didn't remove the the filenames and the time of each slide so you can find them for yourself if you feel like. They are essentially the same thing as stated in this post, but there are also a few new things that I'll mention above the images.
Please note that images are all big. Simply drag them into another tab to view them in their original size
This part is new. Expression Literals
When you have an expression (
M_PI / 16
for example) you should put it inside parenthesis.This syntax works for numeral expressions, booleans, finding an index in a (C-) string, boolean values, enum constants, and even character strings!
More about character strings and how/when you can use this literal syntax:
How array literals work
How dictionary literals work
More on array subscripting
More on dictionary subscripting
[Edit 3]
Mike Ash has a great writeup about these new literals. If you want to know more about this stuff, make sure to check it out.