Chapter 10. Arrays

When you store data, such as numbers or strings, in a variable, you can store only one chunk of data at a time. If you need to store five different numbers, you have to create five different variables. If you need to store 100 different numbers, you have to create 100 different variables. Clearly, having to create a new variable to store each new chunk of data is clumsy and inefficient, so programming languages offer a solution called an array.

An array essentially acts like a single variable that can store multiple chunks of data. Rather than acting like an individual box that can hold only one chunk of data, such as a single variable, an array acts like a big box divided into sections, where each section can store one chunk of data, as shown in Figure 10-1. Instead of being forced to create and name a new variable for each chunk of data you want to store, you just have to create and name a single array to store multiple chunks of data.

An array acts like a single variable that can hold multiple data.

Figure 10-1. An array acts like a single variable that can hold multiple data.

Note

Anything used to store data is called a data structure. An array is the most common type of data structure, but there are other types of data structures for storing data as well. Each type of data structure has its own advantages and disadvantages.

If you're familiar with another programming language, such as C++, you know that most programming languages provide basic commands for creating and manipulating arrays. However, these basic commands often require you to write a lot of code just to do something as simple as adding or removing data from an array.

Arrays in other programming languages also have the limitation of allowing you to store only one type of data in an array. If you wanted to store integers, you would have to create a separate integer array. If you wanted to store floating-point numbers, or strings, you would have to create another floating-point or string array.

To avoid these limitations, Apple has created a special array class, called NSArray. In addition to providing much simpler commands for manipulating arrays, the NSArray class can store different types of data in the same array.

In Mac programming, there are two types of arrays you can create: NSArray and NSMutableArray. The NSArray is known as a staticarray because you can store data in the array once, but never add, change, or remove data later. The NSMutableArray is known as a dynamicarray because you can always add data to, change data within, or delete data from the array while your program is running.

In this chapter, you'll learn how to create arrays, how to store data in an array, how to retrieve data in an array, and how to modify an array by adding or deleting data. Arrays are one of the more powerful tools you can use to keep your program's data organized.

Creating an Array

When you create an array (either an NSArray or NSMutableArray), you're working with objects, so you need to create a pointer such as the following:

NSArray *myArray;
NSMutableArray *myOtherArray;

This simply creates a pointer to an array object. To fill an array with data, you have two options. First, you can declare an array on one line and then fill that array on a second line like this:

NSArray *myArray;
myArray = [NSArray arrayWithObjects: object1,  object2, object3, nil];

A second way to fill an array with data is to declare and initialize the array on a single line:

NSArray *myArray = [NSArray arrayWithObjects: object1,  object2, object3, nil];

Note

When storing items in an array, make sure you always define the end of the array data with nil. If you omit nil, then the computer won't know when your array ends, which will likely cause your program to run incorrectly, if at all.

Although this example adds only three objects, you can add as many objects to an array as you want. Before you can add an object to an array, you must declare that object ahead of the array:

NSString *object1 = @"Hello";
NSString *object2 = @"world!";
NSNumber *object3 = [NSNumber numberWithInt:45];
NSArray *myArray = [NSArray arrayWithObjects: object1,  object2, object3, nil];

Before continuing, it's important to know exactly what's happening in this code. First, whenever you see an asterisk used in front of a variable name, your code is probably defining a pointer, and the presence of pointers almost always means you're working with an object. The first three lines of this example create three objects named object1, object2, and object3. The first two objects are string objects (NSString) and the third object is a number object (NSNumber).

Note

The value object (NSNumber) is used to hold a value such as an integer (int) or floating-point (float) number. In this case, the NSNumber object holds an NSNumber object that contains the number 45.

Second, the square brackets are almost always used to work with objects as well. The equal sign (=) assigns a value to a variable. In this case, the value inside the square brackets gets assigned to the *myArray pointer.

To see how an array works, follow these steps:

  1. Open the VariableTest project from the previous chapter.

  2. Click the VariableTestAppDelegate.m file stored inside the Classes folder. The code for that file appears in the middle pane of the Xcode window.

  3. Modify the code in the VariableTestAppDelegate.m file as follows:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        NSString *object1 = @"Hello";
        NSString *object2 = @"world!";
        NSNumber *object3 = [NSNumber numberWithInt:45];
        NSArray *myArray;
        myArray= [NSArray arrayWithObjects: object1, object2, object3, nil];
        NSLog(@"Array contents = %@",[myArray componentsJoinedByString:@", "]);
    }
  4. Choose File

    Creating an Array
  5. Click the Build and Run button or choose Build

    Creating an Array
  6. Quit your program by clicking the Stop button or choosing Product

    Creating an Array
  7. Choose Run

    Creating an Array
    2010-08-31 22:24:10.948 VariableTest[18411:a0f] Array contents = Hello, world!, 45

Finding the Right Method to Use

When square brackets work with objects, the object name always appears first. In the previous example, the object is NSArray. The next question is, where did the arrayWithObjects method come from?

To find the answer, you need to remember that Apple has defined plenty of useful classes and stored them in its Cocoa framework for all programmers to use. Any time you see a class that begins with the prefix NS (which stands for NeXTSTEP), that's a clue that you're working with one of Apple's predefined classes.

Since classes almost always include properties (data) and methods (subprograms that manipulate the class's data), you can browse through Apple's Developer Documentation to find a list of all the methods and properties available for any class provided by Apple.

To find this documentation, you need to view the Developer Documentation through the Xcode Help menu. Choose Help

Finding the Right Method to Use
The NSArray class reference is available in the Developer Documentation.

Figure 10-2. The NSArray class reference is available in the Developer Documentation.

Scroll through this NSArray class reference and look for a method that lets you store data in the NSArray. You may see several methods, but eventually you'll find the arrayWithObjects method listed, along with an explanation of how it works, as shown in Figure 10-3.

The arrayWithObjects method explained in the Developer Documentation

Figure 10-3. The arrayWithObjects method explained in the Developer Documentation

The general rule when working with classes that begin with an NS prefix is to look for the properties and methods you can use to manipulate those objects. Chances are good that Apple already provides the code you need to use, which means that instead of having to write your own code for adding data to an array (which you have to do in other programming languages), Apple lets you choose the method you need, thus reducing the amount of code you need to write and test.

StoringObjects in an Array

Now that you understand where the arrayWithObjects method came from, you can use it to store objects in the array. You must separate each object with a comma, and when you're done adding objects to the array, you must use the nil value to identify the end of the array. If you don't use nil as the last object stored in your array, you could confuse the computer and cause your program to crash.

Just the simple act of adding data to an array showed you quite a bit about the process of writing a Mac program. The biggest trick is simply knowing which methods, in different classes, you can use and how they work, which you can learn by browsing through the Developer Documentation.

Some other ways to learn about different methods available for specific classes include the following:

  • Have someone show or tell you

  • Find a book or web page that explains that particular method

  • Study Objective-C programs written by someone else

Trying to find the right method to use can be like trying to look up a word in a dictionary when you don't know how to spell that word. The first step should be to look up the class reference for the particular object you want to manipulate, such as NSArray.

Suppose you look up a class reference and can't find the method you want. That doesn't necessarily mean that the method you want doesn't exist. Check the class reference for the object to see a list of other classes that the class inherits from. The NSArray class reference (see Figure 10-2) shows that the NSArray class inherits from the NSObject class. So if the method you want isn't available in the NSArray class reference, it might be described in the NSObject class reference.

Remember, every class contains its own methods and properties. When one class inherits from another class, that second class includes all the methods and properties of both classes. Since the NSArray class inherits from the NSObject class, the NSArray class inherits all the methods and properties from the NSObject class.

If you look up the class reference of one class, such as NSArray, in the Developer Documentation, you can view all the methods and properties unique to that particular class. If you then look up the class that the NSArray class inherits from (NSObject), you can see all the methods and properties specific to the NSObject class. However, since NSArray inherits every property and method from NSObject, NSArray winds up containing every property and method available in both NSObject and NSArray, as shown in Figure 10-4.

Objects inherit methods and properties from other classes.

Figure 10-4. Objects inherit methods and properties from other classes.

Additional Methods for Filling an Array

When creating and filling an array with data, the most common method you'll use to fill the array with objects is the arrayWithObjects method. Of course, there are several other methods for filling an array, such as the following:

  • arrayWithArray: Fills an array with the contents of an existing array

  • arrayWithContentsOfFile: Fills an array with data stored in a file

  • arrayWithContentsOfURL: Fills an array with data retrieved from a URL (web site)

As you can see, every object provides plenty of methods that Apple has already created and tested, so all you have to do is figure out how they work and when to use them in your own program. Using such prewritten and pretested methods helps you build more complicated programs faster and is more reliable than writing and testing your own code.

Counting the Items Stored in an Array

After you have stored data in an array, you can always count the number of items stored in that array by using the count command:

[arrayName count];

The arrayName simply represents the name of your array, and the count command returns the number of items in that array, so the entire line of code evaluates to a single number. If arrayName held six items, the preceding code would return the number 6.

To see how to count items in an array, follow these steps:

  1. Open the VariableTest project from the previous section.

  2. Click the VariableTestAppDelegate.m file stored inside the Classes folder. The code for that file appears in the middle pane of the Xcode window.

  3. Modify the code in the VariableTestAppDelegate.m file as follows:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        NSString *object1 = @"Hello";
        NSString *object2 = @"world!";
        NSNumber *object3 = [NSNumber numberWithInt:45];
        NSArray *myArray;
        myArray= [NSArray arrayWithObjects: object1, object2, object3, nil];
        NSLog(@"Array contents = %@",[myArray componentsJoinedByString:@", "]);
        NSLog (@"Total number of items in array = %i", [myArray count]);
    }
  4. Choose File

    Counting the Items Stored in an Array
  5. Click the Build and Run button or choose Build

    Counting the Items Stored in an Array
  6. Quit your program by clicking the Stop button or choosing Product

    Counting the Items Stored in an Array
  7. Choose Run

    Counting the Items Stored in an Array
    2010-09-02 11:18:09.034 VariableTest[22851:a0f] Array contents = Hello, world!, 45
    2010-09-02 11:18:09.038 VariableTest[22851:a0f] Total number of items in array = 3

Accessing an Item in an Array

When you store items in an array, the first item gets stored at one end of the array, the second item gets stored next to the first item, the third item gets stored next to the second item, and so on.

To keep track of all the items stored in an array, the array uses a number called an array index. The first item in an array is stored at index 0, the second item in an array is stored at index 1, and so on, as shown in Figure 10-5.

The array index identifies an item's position in an array.

Figure 10-5. The array index identifies an item's position in an array.

Note

In Objective-C, the first item in an array is at index 0. Such arrays are known as zero-based arrays. In other programming languages, the first item in an array may start at index 1. Such arrays are known as one-based arrays. Most programming languages based on C, such as Objective-C, use zero-based arrays.

To retrieve an item from an array, you need to identify that item's index position in the array. So if you wanted to retrieve the first item in the array, you would retrieve the item stored at index position 0, if you wanted to retrieve the second item in the array, you would retrieve the item stored at index position 1, and so on.

To retrieve a specific item from an array, you have to identify the array, use the objectAtIndex method, and specify the index position:

[arrayName objectAtIndex: index];

The preceding code would return whatever object was stored in the array at the designated index position. So if you created an array like this,

NSArray *myArray = [NSArray arrayWithObjects: @"Hello", @"world", "@Good-bye", nil];

you could use the following code to retrieve the second item (index position 1) in the array, which would be "world":

[myArray objectAtIndex:1];

To see how to retrieve items from an array, follow these steps:

  1. Open the VariableTest project from the previous section.

  2. Click the VariableTestAppDelegate.m file stored inside the Classes folder. The code for that file appears in the middle pane of the Xcode window.

  3. Modify the code in the VariableTestAppDelegate.m file as follows:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
       NSString *object1 = @"Hello";
        NSString *object2 = @"world!";
        NSNumber *object3 = [NSNumber numberWithInt:45];
        NSArray *myArray;
        myArray= [NSArray arrayWithObjects: object1, object2, object3, nil];
        NSLog(@"Array contents = %@",[myArray componentsJoinedByString:@", "]);
         NSLog (@"Index position 1 = %@", [myArray objectAtIndex:1]);
    }
  4. Choose File

    The array index identifies an item's position in an array.
  5. Click the Build and Run button or choose Build

    The array index identifies an item's position in an array.
  6. Quit your program by clicking the Stop button or choosing Product

    The array index identifies an item's position in an array.
  7. Choose Run

    The array index identifies an item's position in an array.
    2010-09-03 14:53:37.017 VariableTest[26555:a0f] Array contents = Hello, world!, 45
    2010-09-03 14:53:37.021 VariableTest[26555:a0f] Index position 1 = world!

Accessing All Items in an Array

Accessing an individual item in an array is fine when you only want that one item. However, if you want to retrieve all the items in an array, and all items stored in the array are of the same data type, you can use something called fast enumeration.

Fast enumeration lets you access every item in an array by using a modified for loop that looks like this:

for (objectType *variable in arrayName)

The objectType represents the type of objects stored in an array, such as NSString or NSNumber. The *variable is any arbitrarily named variable you want to use. The arrayName is the actual array. Suppose you had an array like this:

NSString *object1 = @"Hello";
NSString *object2 = @"world!";
NSString *object3 = @"Good-bye";
NSArray *myArray = [NSArray arrayWithObjects: object1, object2, object3, nil];

To retrieve every item out of that array using fast enumeration, you could use a for loop like this:

for (NSString *randomVariable in myArray)
    {
NSLog (@"Array element = %@", randomVariable);
    }

To see how fast enumeration works, follow these steps:

  1. Open the VariableTest project from the previous section.

  2. Click on the VariableTestAppDelegate.m file stored inside the Classes folder. The code for that file appears in the middle pane of the Xcode window.

  3. Modify the code in the VariableTestAppDelegate.m file as follows:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        NSString *object1 = @"Hello";
        NSString *object2 = @"world!";
        NSString *object3 = @"Good-bye";
        NSArray *myArray;
        myArray= [NSArray arrayWithObjects: object1, object2, object3, nil];
        for (NSString *randomVariable in myArray)
        {
           NSLog (@"Array element = %@", randomVariable);
        }
    }
  4. Choose File

    Accessing All Items in an Array
  5. Click the Build and Run button or choose Build

    Accessing All Items in an Array
  6. Quit your program by clicking the Stop button or choosing Product

    Accessing All Items in an Array
  7. Choose Run

    Accessing All Items in an Array
    2010-09-03 15:25:59.460 VariableTest[26668:a0f] Array element = Hello
    2010-09-03 15:25:59.461 VariableTest[26668:a0f] Array element = world!
    2010-09-03 15:25:59.463 VariableTest[26668:a0f] Array element = Good-bye

If you have stored different types of data in an array, then you can access each item in your array using a for loop that starts at 0 and runs for each item stored in the array using the count method:

int i;
for (i = 0; i < [arrayName count]; i++)
{
     NSLog (@"Element %i = %@", i, [arrayName objectAtIndex: i]);
}

This for loop runs from 0 to the total number of items stored in the array. If you had three items stored in the array, the for loop would run five times (from 0 to 2).

To see how to retrieve data from an array using an ordinary for loop, follow these steps:

  1. Open the VariableTest project from the previous section.

  2. Click on the VariableTestAppDelegate.m file stored inside the Classes folder. The code for that file appears in the middle pane of the Xcode window.

  3. Modify the code in the VariableTestAppDelegate.m file as follows:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        NSString *object1 = @"Hello";
        NSString *object2 = @"world!";
        NSNumber *object3 = [NSNumber numberWithInt:45];
        NSArray *myArray;
        myArray= [NSArray arrayWithObjects: object1, object2, object3, nil];
        int i;
        for (i = 0; i < [myArray count]; i++)
        {
            NSLog (@"Element %i = %@", i, [myArray objectAtIndex: i]);
        }
    }
  4. Choose File

    Accessing All Items in an Array
  5. Click the Build and Run button or choose Build

    Accessing All Items in an Array
  6. Quit your program by clicking the Stop button or choosing Product

    Accessing All Items in an Array
  7. Choose Run

    Accessing All Items in an Array
    2010-09-03 21:13:43.781 VariableTest[27527:a0f] Element 0 = Hello
    2010-09-03 21:13:43.784 VariableTest[27527:a0f] Element 1 = world!
    2010-09-03 21:13:43.791 VariableTest[27527:a0f] Element 2 = 45

Adding Items to an Array

When you declare an array, you have to declare it as either an NSArray or NSMutableArray. If you declare an array as an NSArray, you can store data in it once, but never again. If you want your program to be able to store and change data in an array while your program runs, you need to declare your array as an NSMutableArray like this:

NSMutableArray *arrayName;

To add data to an NSMutableArray, you need to define the NSMutableArray to use, use the addObject method, and specify the data to add:

[arrayName addObject: newObject];

Suppose you had the following NSMutableArray and filled it with data like this:

NSString *object1 = @"Hello";
NSString *object2 = @"world!";
NSString *object3 = @"Good-bye";
NSMutableArray *myArray;
myArray= [NSMutableArray arrayWithObjects: object1, object2, object3, nil];

You could add data to this array by using code like this:

[myArray addObject: @"New item"];

This would add the object @"New item" at the end of the array named myArray. To see how the addObject method works, follow these steps:

  1. Open the VariableTest project from the previous section.

  2. Click the VariableTestAppDelegate.m file stored inside the Classes folder. The code for that file appears in the middle pane of the Xcode window.

  3. Modify the code in the VariableTestAppDelegate.m file as follows:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        NSString *object1 = @"Hello";
        NSString *object2 = @"world!";
        NSString *object3 = @"Good-bye";
        NSMutableArray *myArray;
        myArray= [NSMutableArray arrayWithObjects: object1, object2, object3, nil];
        for (NSString *randomVariable in myArray)
        {
           NSLog (@"Array element = %@", randomVariable);
        }
        [myArray addObject: @"New item"];
        NSLog (@"**********");
        for (NSString *randomVariable in myArray)
        {
            NSLog (@"Array element = %@", randomVariable);
        }
    }
  4. Choose File

    Adding Items to an Array
  5. Click the Build and Run button or choose Build

    Adding Items to an Array
  6. Quit your program by clicking the Stop button or choosing Product

    Adding Items to an Array
  7. Choose Run

    Adding Items to an Array
    2010-09-03 16:14:56.614 VariableTest[26819:a0f] Array element = Hello
    2010-09-03 16:14:56.614 VariableTest[26819:a0f] Array element = world!
    2010-09-03 16:14:56.615 VariableTest[26819:a0f] Array element = Good-bye
    2010-09-03 16:14:56.616 VariableTest[26819:a0f] **********
    2010-09-03 16:14:56.617 VariableTest[26819:a0f] Array element = Hello
    2010-09-03 16:14:56.619 VariableTest[26819:a0f] Array element = world!
    2010-09-03 16:14:56.620 VariableTest[26819:a0f] Array element = Good-bye
    2010-09-03 16:14:56.633 VariableTest[26819:a0f] Array element = New item

The first time the for loop runs, it prints all three items in the array (Hello, world!, and Good-bye). The second time the for loops runs, it prints four items stored in the array (Hello, world!, Good-bye, and New item). The addObject method always adds the new item at the end of the array.

Inserting Items into an Array

The addObject method is fine when you just want to store an item at the end of the array. However, what if you want to store an item in a specific part of an array, such as the beginning or somewhere in the middle? In that case, you have to use the insertObject method, which looks like this:

[arrayName insertObject: newObject atIndex: index];

The main difference between the addObject method and the insertObject method is that when you use the insertObject method, you must also specify the index position where you want to add the new item. If you wanted to insert a new item as the second item in the array, which would be index position 1, you would do so like this:

[arrayName insertObject: newObject atIndex: 1];
[myArray addObject: @"New item"];

This would add the object @"New item" at the end of the array named myArray. To see how the addObject method works, follow these steps:

  1. Open the VariableTest project from the previous section.

  2. Click on the VariableTestAppDelegate.m file stored inside the Classes folder. The code for that file appears in the middle pane of the Xcode window.

  3. Modify the code in the VariableTestAppDelegate.m file as follows:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        NSString *object1 = @"Hello";
        NSString *object2 = @"world!";
        NSString *object3 = @"Good-bye";//[NSNumber numberWithInt:45];
        NSMutableArray *myArray;
        myArray= [NSMutableArray arrayWithObjects: object1, object2, object3, nil];
        for (NSString *randomVariable in myArray)
        {
           NSLog (@"Array element = %@", randomVariable);
        }
        [myArray insertObject: @"New item" atIndex: 1];
        NSLog (@"**********");
        for (NSString *randomVariable in myArray)
        {
            NSLog (@"Array element = %@", randomVariable);
        }
    }
  4. Choose File

    Inserting Items into an Array
  5. Click the Build and Run button or choose Build

    Inserting Items into an Array
  6. Quit your program by clicking the Stop button or choosing Product

    Inserting Items into an Array
  7. Choose Run

    Inserting Items into an Array
    2010-09-03 19:00:06.133 VariableTest[27100:a0f] Array element = Hello
    2010-09-03 19:00:06.136 VariableTest[27100:a0f] Array element = world!
    2010-09-03 19:00:06.137 VariableTest[27100:a0f] Array element = Good-bye
    2010-09-03 19:00:06.139 VariableTest[27100:a0f] **********
    2010-09-03 19:00:06.144 VariableTest[27100:a0f] Array element = Hello
    2010-09-03 19:00:06.144 VariableTest[27100:a0f] Array element = New item
    2010-09-03 19:00:06.145 VariableTest[27100:a0f] Array element = world!
    2010-09-03 19:00:06.146 VariableTest[27100:a0f] Array element = Good-bye

Just remember that when you specify an index position for inserting an item into an array, that index position must be available. So, for example, if you have an array that consists of three items, which would correspond to index positions 0, 1, and 2, and you try to insert a new item at any index position that's 3 or higher, your program won't work.

Deleting Items from an Array

Just as you can add and insert new items into an array, you can delete items from an array. When you delete an item, you must specify the index position of the item that you want to delete.

There are actually several methods you can use to delete an item from an array:

  • removeLastObject: Deletes the last item in the array

  • removeObjectAtIndex: Deletes an item at a specific index position

  • removeAllObjects: Deletes everything from an array

  • removeObject: Deletes all instances of an item stored in an array

Deleting the Last Item in an Array

When you just want to delete the last item in an array, you need to specify the array name and use the removeLastObject method like this:

[arrayName removeLastObject];

Deleting an Item from a Specific Index Position

Another way to delete an item from an array is to specify the index position of the item that you want to delete using this code:

[arrayName removeObjectAtIndex: index];

So if you wanted to delete the second item in an array, you would specify index position 1:

[arrayName removeObjectAtIndex: 1];

Deleting Every Item from an Array

If you need to clear out an entire array, you could delete items one at a time, but it's much faster to wipe out everything at once using the removeAllObjects method like this:

[arrayName removeAllObjects];

Deleting All Instances of an Item from an Array

It's possible to store identical data in different parts of an array. For example, you might store the string @"Hello" in the first index position of an array and also in the sixth index position of that same array.

If you want to delete all @"Hello" strings in an array, you can use the removeObject method and specify the data that you want to delete:

[arrayName removeObject: object];

So if you wanted to delete the @"Hello" string from an array, you would use this code:

[arrayName removeObject: @"Hello"];

To see all the different ways to delete items from an array, follow these steps:

  1. Open the VariableTest project from the previous section.

  2. Click the VariableTestAppDelegate.m file stored inside the Classes folder. The code for that file appears in the middle pane of the Xcode window.

  3. Modify the code in the VariableTestAppDelegate.m file as follows:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        NSString *object1 = @"Hello";
        NSString *object2 = @"world!";
        NSString *object3 = @"Good-bye";
        NSString *object4 = @"Hello";
        NSString *object5 = @"More data";
        NSString *object6 = @"Hello";
        NSString *object7 = @"Last data";
        NSMutableArray *myArray;
        myArray= [NSMutableArray arrayWithObjects: object1, object2, object3, object4,
    object5, object6, object7, nil];
        NSLog (@"***** Original array *****");
        for (NSString *randomVariable in myArray)
        {
           NSLog (@"Array element = %@", randomVariable);
        }
    
        NSLog (@" ");
        NSLog (@"***** Deleting last array item *****");
        [myArray removeLastObject];
        for (NSString *randomVariable in myArray)
        {
            NSLog (@"Array element = %@", randomVariable);
        }
    NSLog (@" ");
        NSLog (@"***** Deleting item at index position 2 *****");
        [myArray removeObjectAtIndex: 2];
        for (NSString *randomVariable in myArray)
        {
            NSLog (@"Array element = %@", randomVariable);
        }
    
        NSLog (@" ");
        NSLog (@"***** Deleting all instances of Hello *****");
        [myArray removeObject: @"Hello"];
        for (NSString *randomVariable in myArray)
        {
            NSLog (@"Array element = %@", randomVariable);
        }
    }
  4. Choose File

    Deleting All Instances of an Item from an Array
  5. Click the Build and Run button or choose Build

    Deleting All Instances of an Item from an Array
  6. Quit your program by clicking the Stop button or choosing Product

    Deleting All Instances of an Item from an Array
  7. Choose Run

    Deleting All Instances of an Item from an Array
    2010-09-03 20:48:16.715 VariableTest[27435:a0f] ***** Original array *****
    2010-09-03 20:48:16.725 VariableTest[27435:a0f] Array element = Hello
    2010-09-03 20:48:16.726 VariableTest[27435:a0f] Array element = world!
    2010-09-03 20:48:16.726 VariableTest[27435:a0f] Array element = Good-bye
    2010-09-03 20:48:16.727 VariableTest[27435:a0f] Array element = Hello
    2010-09-03 20:48:16.729 VariableTest[27435:a0f] Array element = More data
    2010-09-03 20:48:16.730 VariableTest[27435:a0f] Array element = Hello
    2010-09-03 20:48:16.731 VariableTest[27435:a0f] Array element = Last data
    2010-09-03 20:48:16.732 VariableTest[27435:a0f]
    2010-09-03 20:48:16.738 VariableTest[27435:a0f] ***** Deleting last array item *****
    2010-09-03 20:48:16.740 VariableTest[27435:a0f] Array element = Hello
    2010-09-03 20:48:16.741 VariableTest[27435:a0f] Array element = world!
    2010-09-03 20:48:16.742 VariableTest[27435:a0f] Array element = Good-bye
    2010-09-03 20:48:16.742 VariableTest[27435:a0f] Array element = Hello
    2010-09-03 20:48:16.743 VariableTest[27435:a0f] Array element = More data
    2010-09-03 20:48:16.744 VariableTest[27435:a0f] Array element = Hello
    2010-09-03 20:48:16.758 VariableTest[27435:a0f]
    2010-09-03 20:48:16.759 VariableTest[27435:a0f] ***** Deleting item at index position 2
    *****
    2010-09-03 20:48:16.760 VariableTest[27435:a0f] Array element = Hello
    2010-09-03 20:48:16.761 VariableTest[27435:a0f] Array element = world!
    2010-09-03 20:48:16.762 VariableTest[27435:a0f] Array element = Hello
    2010-09-03 20:48:16.763 VariableTest[27435:a0f] Array element = More data
    2010-09-03 20:48:16.764 VariableTest[27435:a0f] Array element = Hello
    2010-09-03 20:48:16.764 VariableTest[27435:a0f]
    2010-09-03 20:48:16.765 VariableTest[27435:a0f] ***** Deleting all instances of Hello *****
    2010-09-03 20:48:16.766 VariableTest[27435:a0f] Array element = world!
    2010-09-03 20:48:16.766 VariableTest[27435:a0f] Array element = More data

Summary

Arrays let you store multiple chunks of data in a single variable. To create an array, you have to create a pointer to an array based on the NSArray or NSMutableArray class. If you just want to store data in an array, use the NSArray class. If you want your program to be able to add data to or delete data from an array while your program is running, use the NSMutableArray class.

Arrays can store different types of data or the same type of data. If you store the same type of data in an array, you can use fast enumeration to access every item in your array. If you store different types of data in an array, you need to use a traditional for loop to retrieve each item in the array.

You can add an item to an NSMutableArray at the end of that array or at a specific index position. You can also delete data from an array in several ways: delete data from a specific position in the array (either from the end of the array or at a specific index position), delete one or more instances of data stored in the array, or delete all items from an array. Arrays are just one way to store multiple, related chunks of data in a single variable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.107.1