Objective-C: How to create an NSArray Object in standard way and collection literal way?
1 min readJul 23, 2022
NSArray is part of the collections in Objective-C. It allows retrieving an object by using an index from an Array. NSArray can also be represented using collection literal in Objective-C.
Let’s look at how to create an NSArray in Objective-C,
- The standard way to create an NSArray,
NSArray *standardWay = [NSArray arrayWithObjects: @"hello", @"world", nil];
- The collection literal way to create an NSArray,
NSArray *collectionLiteralWay = @[@"hello", @"world"];
Let’s look at how to access an object from NSArray in Objective-C,
- The standard way to access an object in NSArray,
[standardWay objectAtIndex:0];
// output is
// hello
- The collection literal way to access an object in NSArray,
collectionLiteralWay[0];
// output is
// hello
Note: The index in an array starts at 0.
I hope my article helps you understand how to create an NSArray object in Objective-C.
If you like my articles, please follow me. You can also support me by buying me a coffee.