-2

For example:

[[FlyoutTableViewController alloc] initWithViewControllers:@[@[firstNavigationController],@[secondNavigationController]]];

- (id)initWithViewControllers:(NSArray *)viewControllers

I know it's sending a NSArray of these view controllers to initWithViewControllers, but I want to know more about this way of creating arrays. What are the benifits, any potential issues etc?

Also, do the objects, firstNavigationController and secondNavigationController need to be inside the @[ too, or what's going on there?

TIA

Barry Mc S
  • 13
  • 1

5 Answers5

0

@[item1, item2, item3] is syntax sugar for [NSArray arrayWithObjects:item1, item2, item3, nil]. Nothing more than a shorthand. There are no more benefits or issues than with the method it expands to.

You can make arrays of arrays too like that. @[@[item1]] is an array containing a single array containing item1.

zneak
  • 134,922
  • 42
  • 253
  • 328
  • 1
    Well, to be precise, it is equivalent to `+[NSArray arrayWithObjects:count:]`, compare e.g. http://stackoverflow.com/questions/14527489/using-array-of-items-vs-nsarray-arraywithobjects. – Martin R Jan 12 '14 at 18:10
0

It means you are defining an array with a values. e.g.

@[value1, value2, value3]
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
0

It's an array literal. Like a string literal @"My String". A shorthand notation for creating arrays. Similar usage and limitations to the NSString version (immutable).

So, @[firstNavigationController] creates an NSArray with a single item in it. And your parameter is an array of arrays.

Wain
  • 118,658
  • 15
  • 128
  • 151
0

It is a shorthand way of creating an NSArray.

NSArray *array = [[NSArray alloc] init];

is equivalent to

NSArray *array = @[];

You can see more objective-c literals here.

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
0

Let me give you the analogy.

"A String" : C-string

@"A String": Obj-C string (NSString)


{1,2} (not [1,2]): C array

@[@1, @2] (not @[1,2]): Obj-C array (NSArray with NSNumber elements)

mostruash
  • 4,169
  • 1
  • 23
  • 40