Using @[array, of, items] vs [NSArray arrayWithObjects:]

They are almost identical, but not completely. The Clang documentation on Objective-C Literals states: Array literal expressions expand to calls to +[NSArray arrayWithObjects:count:], which validates that all objects are non-nil. The variadic form, +[NSArray arrayWithObjects:] uses nil as an argument list terminator, which can lead to malformed array objects. So NSArray *myArray = @[objectOne, objectTwo, … Read more

Can the new Clang Objective-C literals be redirected to custom classes?

You can substitute class for some Objective-C literals with @compatibility_alias keyword trick. Here’s an example. @compatibility_alias NSNumber AAA; Of course, you should provide proper implementation for new class. #import <Foundation/NSObject.h> @interface AAA : NSObject + (id)numberWithInt:(int)num; @end @implementation AAA + (id)numberWithInt:(int)num { return @”AAAAA!!!”; // Abused type system just to check result. } @end @compatibility_alias … Read more

Compiler error “expected method not found” when using subscript on NSArray

You’ve got to be compiling with the iOS 6 or OS X 10.8 SDKs — otherwise Foundation objects don’t have the necessary methods for the subscripting bit of the literal syntax.* Specifically in this case, the subscripting expects objectAtIndexedSubscript: to be implemented by NSArray, and that’s a new method that was created to interact with … Read more

Is there some literal dictionary or array syntax in Objective-C?

With this change to the LLVM codebase, Apple has added a new syntax for literals in upcoming versions of the Clang compiler. Before, arrays were created using a C-based array and were converted on the fly into Objective-C objects, such as: NSArray* array = [NSArray arrayWithObjects: @”One”, @”Two”, @”Three”, nil]; Note that since this is … Read more

What are the details of “Objective-C Literals” mentioned in the Xcode 4.4 release notes?

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: array = [NSArray arrayWithObjects:a, b, c, nil]; Now: array = @[ a, b, c ]; NSDictionary Literals Previously: dict = [NSDictionary dictionaryWithObjects:@[o1, o2, o3] forKeys:@[k1, k2, k3]]; Now: … Read more