iOS Development

Course No. 10-152-139

Chapter 17 — NSArray

We use this one a lot.


  • A simple array is a series of values in a row.

  • In C and Objective-C we start with zero to number the elements in an array.
  • We can also have an array of pointers.


  • An NSArray is an object that holds pointers to other objects.

  • An NSArray is immutable. This is a common programming term that is not a common English word.
    • im•mu•ta•ble
      unchanging over time or unable to be changed: an immutable fact.
  • Once an NSArray has been created it can’t be changed. We can’t add things to it and we can’t remove anything.
  • We create an NSArray with a message to the class. In this example we’ll use Objective-C strings as the objects.
    • The nil is required with this message.
  • Getting the number of objects in an NSArray.
  • Accessing one object in an array.
  • Looping through an NSArray.

Modern Objective-C

There is a new way to make arrays in the version of Objective-C that came out with Xcode 4.4. They are called Objective-C Literals. This is touched on in the 2nd edition of the book. We will explore this new syntax in sections marked with the Modern Objective-C header in greater depth.

  • Create an NSArray with modern Objective-C literals.
  • A little more detail, please! The array is created with the @ and a set of square brackets.
  • We add the objects inside the brackets. We can also add pointers to objects here.
  • Example with pointers.
  • We don’t use a nil in this syntax.
  • Accessing an object in an NSArray with the new syntax.
  • Details of the new access syntax. Start with the name of the pointer to the NSArray. Add a set of square brackets.
  • The number inside the brackets is the index of the object in the NSArray.


  • The NSMutableArray is what you might guess. It’s an array that’s changeable. You can add and delete objects.
  • You can create one without any objects and add them later. And then remove them.

Modern Objective-C for NSMutableArray

  • We can’t make an NSMutableArray with the literal syntax. We still have to use methods for this. However, we can assign different objects to elements in the array.

A Challenging Challenge!

  • The second challenge starting on page 127 is, as the book says, “challenging”. Let’s look at the starting example given in the book in more detail.
  • Create a new project named Chapter17ChallengeStart. Copy and paste this into the new project and get it to run.
  • Let’s split it up and look at what’s it doing.
    • This statement opens the file named propernames in the directory /usr/share/dict/. This directory is on all Macs with a current version of OS X installed. The entire contents of the file is read and assigned to the string pointer named nameString.
    • Here’s a small part of the file. As you can see, each name is on its own line.
    • Next, an NSArray is created by breaking up the string by the line feed character.
    • Next, the for loop iterates through the NSArray.
      • If the string @"AA" exists in a name we output it.

What do we have to do?

  1. Open and load the file /usr/share/dict/words instead of propernames.
  2. Create an NSArray just like the example.
  3. Compare the current word to either the next word or the previous word. Both approaches will work just fine. How would each work?
    • The comparison will be done with the case insensitive method shown in the chapter 16 notes.