Search code examples
objective-cxcode4.4llvm-4.0

use '[]' syntax to access containers in XCode 4.4 seems not working


Has anyone tried new syntax sugar introduced in Xcode 4.4 (iOS 5.1), like automatically calls @synthesize or Literal Syntax for NSArray ? They are quite handy.

But I can't get it right for this one , "use '[]' syntax to access". I tried following and they did not work. What did I do wrong ? Thanks.

NSArray *tmp = @[@"hello",@"world"];  //This one works fine
NSString *i = tmp[0]; // or tmp[@0];  this one does not work.

Solution

  • You're using two different features there. Your first line (tmp = @[@"hello",@"world"]) are literals. That should work in Xcode 4.4.

    The second line (i = tmp[0]) needs runtime support (there are a couple of extra methods needed to make it work) and so won't work in iOS 5 and below. See this answer for more details.