Working with Arrays
NSArray and NSMutableArray objects manage immutable and mutable collections of objects, respectively. Each has the following attributes:
- A count of the number of objects in the array
- The objects contained in the array
You can create NSArrays with WebScript's @ syntax for defining constant objects. For example, the following statements create NSArrays:
id availableQuantities = @(1, 6, 12, 48); id shortWeekDays = @("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");You can also create NSArrays with creation methods. If you want to create a static array that contains variables, you have to use a creation method because you can't use variables in WebScript's @ syntax. The following statement creates an NSArray that contains variables:
id dinnerPreferences = [NSArray arrayWithObjects:firstChoice, secondChoice, nil];The variable dinnerPreferences is an NSArray, so its initial collection of objects can't be added to or subtracted from. When you need to create an array that can be modified, use a creation method to create an NSMutableArray. For example, the following statement creates an empty NSMutableArray to which you can add objects:
id mutableArray = [NSMutableArray array];The methods provided by NSArray and NSMutableArray are described in more detail in the next section, "Commonly Used Array Methods."
Table of Contents Next Section