Objective-C Language Subscripting Subscripts with NSArray

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


Subscripts can be used to simplify retrieving and setting elements in an array. Given the following array

NSArray *fruit = @[@"Apples", @"Bananas", @"Cherries"];

This line

[fruit objectAtIndex: 1];

Can be replaced by


They can also be used to set an element in a mutable array.

NSMutableArray *fruit = [@[@"Apples", @"Bananas", @"Cherries"] mutableCopy];
fruit[1] = @"Blueberries";
NSLog(@"%@", fruit[1]); //Blueberries

If the index of the subscript equals the count of the array, the element will be appended to the array.

Repeated subscripts may be used to access elements of nested arrays.

NSArray *fruit = @[@"Apples", @"Bananas", @"Cherries"];
NSArray *vegetables = @[@"Avocado", @"Beans", @"Carrots"];
NSArray *produce = @[fruit, vegetables];
NSLog(@"%@", produce[0][1]); //Bananas

Got any Objective-C Language Question?