As a Cocoa developer, you are provided with many classes that can help you group several of your objects together in specific ways. In this chapter, we discuss the main collection classes available to you.
The chapter is organized as follows. In Section 2.1, we address the topic of arrays. You will learn about immutable and mutable arrays, the different approaches used for copying arrays, and several sorting techniques. Section 2.2 covers the topic of sets. Sets are collections that do not impose ordering on the objects they contain. You will learn about immutable and mutable sets as well as several interesting operations on sets. In Section 2.3, we discuss dictionaries. Dictionaries allow you to store objects and retrieve them using keys. As you have seen in Section 1.7, dictionaries are widely used in Cocoa frameworks and understanding them is essential. Finally, we provide a summary in Section 2.4.
NSMutableArray if you would like to store/access your objects in an ordered way.
NSArray is the immutable version of the array, allowing you to store your objects only once (during initialization).
NSMutableArray is a subclass of
NSArray that allows you to add/remove objects even after initialization of the collection.
To help illustrate the main concepts behind these two classes, let us use the simple class
Person shown in Listing 2.1.
Example 2.1. The class
Person used in the arrays examples.