Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top