Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

1. The Basics > 1.25. Allocating and Making Use of Sets

1.25. Allocating and Making Use of Sets

Problem

You would like to store an array of objects but you don’t want any one object to appear more than once in the array.

Solution

Use sets instead of arrays.

Discussion

Sets are very similar to arrays. The big difference is that sets allow objects to be added only once. The second time you try to add the same object, it will be rejected by the set. We use NSSet for immutable and NSMutableSet for mutable sets. Let’s have a look at an example of an immutable set:

NSString *hisName = @"Robert";
NSString *hisLastName = @"Kiyosaki";

NSString *herName = @"Kim";
NSString *herLastName = @"Kiyosaki";

NSSet *setOfNames = [[NSSet alloc] initWithObjects:
                     hisName,
                     hisLastName,
                     herName,
                     herLastName, nil];

NSLog(@"Set = %@", setOfNames);

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial