Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 7 / 68

Working with arrays

This lesson covers the basics of NSArray in Objective-C, with the aim of handling several pieces of data together.

An array is like a shelf of numbered drawers. The @[ ] notation creates an NSArray.

The sample code builds an array with NSArray *fruits = @[@"apple", @"orange", @"grape"] and displays each element with a number by combining a for statement with fruits.count. The @[ ] literal is Objective-C's concise way of writing an array.

A common early stumble is the difference between NSArray and NSMutableArray. An NSArray cannot be changed after it is created; to add or remove elements you need an NSMutableArray.

In professional work, using an immutable NSArray for data that does not need to change gives you a safer design that prevents unintended modification.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)