0

I happend to see one particular code,

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

What's the name of annotation @[indexPath], I never see this kind. and since when it introduces in objective-C. I know it replaces [NSArray arrayWithObjects:indexPath,nil], any other functions of that? what the feature to use this (well, other than shorter)?

Thanks.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
HelmiB
  • 12,303
  • 5
  • 41
  • 68

2 Answers2

2

That is an extension to the "Literals" available in Objective-C with LLVM. I don't believe it does anything else apart from create an array. They became available with Apple LLVM 4.0.

If you'd like to see all the literals available, check out http://clang.llvm.org/docs/ObjectiveCLiterals.html - they're quite handy.

rickerbh
  • 9,731
  • 1
  • 31
  • 35
1

It is new way to use literals in Xcode 4.4 No other benefit I guess but its new style of coding

Few references are as, I hope this will clear few of your doubts.

int a = 2;
int b = 5;
NSNumber *n = @(a*b);

@blah is called the "literal" syntax. You use it to make objects wrapping a literal, like a char, BOOL, int, etc. that means:

  • @42 is a boxed int
  • @'c' is a boxed char
  • @"foo" is a boxed char*
  • @42ull is a boxed unsigned long long
  • @YES is a boxed BOOL

All of the things following the at sign are primitive values. MyEnumValue is not a literal. It's a symbol. To accommodate this, generic boxing syntax was introduced:

@(MyEnumValue)

You can put a bunch of things inside the parentheses; for the most part, any sort of variable or expression ought to work.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Literals were introduced in [Xcode 4.4](http://developer.apple.com/library/mac/ipad/#documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_4.html). – Adam Nov 12 '12 at 06:30
  • +1 for the answer. And it was introduced in 4.4 as mentioned above. – iDev Nov 12 '12 at 06:32
  • Object literals may be faster and are certainly less error-prone. The big advantage is that `arrayWithObjects:` and similar methods require a `nil` at the end to tell them where the list ends; with a literal, you do not end the list with `nil`, so that's one fewer thing to forget. – Peter Hosey Nov 14 '12 at 00:00