4

I have seen

NSArray *objectsToShare = @[objects];

when looking at some example code.

What is the meaning of @[objects] here ?

Sumit Mundra
  • 3,891
  • 16
  • 29
SandeepM
  • 2,601
  • 1
  • 22
  • 32

4 Answers4

8
NSArray *objectsToShare = @[objects];

is the same as

NSArray *objectsToShare = [NSArray arrayWithObjects:objects count:count];
DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • 4
    Strictly speaking, it is not the same as `initWithObjects`, but `arrayWithObjects:count:` (compare http://stackoverflow.com/a/14527582/1187415). It returns an autoreleased object, and the treatment of `nil` in the list is different. – Martin R Mar 26 '13 at 11:20
6

It is also known as Literals in Objective-C

Examples
Immutable array expression:
NSArray *array = @[ @"Hello", NSApp, [NSNumber numberWithInt:42] ];

When using Apple LLVM compiler 4.0 or later, arrays, dictionaries, and numbers (NSArray, NSDictionary, NSNumber classes) can also be created using literal syntax instead of methods.[22] Literal syntax uses the @ symbol combined with [], {}, (), .

Example without literals:

NSArray *myArray = [NSArray arrayWithObject:someObject];
NSDictionary *myDictionary = [NSDictionary dictionaryWithObject:someObject forKey:@"key"];
NSNumber *myNumber = [NSNumber numberWithInt:myInt];

Example with literals:

NSArray *myArray = @[ someObject ];
NSDictionary *myDictionary = @{ @"key" : someObject };
NSNumber *myNumber = @(myInt);


objc-at-expression : '@' (string-literal | encode-literal | selector-literal | protocol-literal | object-literal)
                   ;

object-literal : ('+' | '-')? numeric-constant
               | character-constant
               | boolean-constant
               | array-literal
               | dictionary-literal
               ;

boolean-constant : '__objc_yes' | '__objc_no' | 'true' | 'false'  /* boolean keywords. */
                 ;

array-literal : '[' assignment-expression-list ']'
              ;

assignment-expression-list : assignment-expression (',' assignment-expression-list)?
                           | /* empty */
                           ;

dictionary-literal : '{' key-value-list '}'
                   ;

key-value-list : key-value-pair (',' key-value-list)?
               | /* empty */
               ;

key-value-pair : assignment-expression ':' assignment-expression
               ;

For more info Read this Tutorial

Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • 1
    @Downvoter please leave comment on why the down vote then user can improve answer where needed. – Popeye Mar 26 '13 at 11:30
  • 1
    +1 from me as your answers are generally pretty good and I can't see anything wrong with this so don't believe you deserve a downvote for it. – Popeye Mar 26 '13 at 11:32
  • 1
    Good generalized explanation which helps readers to understand that this isn't relegated to arrays. Thanks. – Jeremy Mar 26 '13 at 13:48
0

It makes a array with one object thats the object "objects"

bogen
  • 9,954
  • 9
  • 50
  • 89
0

It is a new feature added to the LLVM compiler. You can create an array with

NSArray *array = @[object1, ...];

Note that you cannot create a mutable array and that you do not need to end the list of objects with nil. Watch the WWDC 2012 video "What's New in LLVM".

ColemanCDA
  • 964
  • 7
  • 13