I saw an Apple sample code with a NSDictionary being initialized using @{ value:key } notation. I use to initialize a constant NSDictionary using +dictionaryWithObjectsAndKeys:
My question is:
Is there any difference between the two generated NSDictionaries? Do I need to worry about memory leaking? I’m using ARC.
All I found about it is this Apple doc, but it is related to Mac, not iOS. And the notation is @{ key = value } and not @{ value:key }.
A second question would be: is that safe to use this to submit the app to App Store, or would it be considered “undocumented API”?
Thanks!
The compiler uses
+[NSDictionary dictionaryWithObjects:forKeys:count:]. So the ‘gotcha’ is that the parameters/values you use in these literal expression must not benil. When you use+[NSDictionary dictionaryWithObjectsAndKeys:], the input stops when nil is encountered. It is an error to passnilto a literal expression as a key or value to a literal expression. This may change your program (because semantics of dictionary creation through va_lists are different), but the stricter semantics would likely result in detecting bugs, more than anything.You might need an autorelease pool in some cases — that depends on the context you create it in.
You will need Apple-Clang 4.0 (Xcode 4.4). It is compatible with all versions of OS X and iOS:
http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/ObjCAvailabilityIndex/_index.html
More details here: http://clang.llvm.org/docs/ObjectiveCLiterals.html