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

Chapter 8: Programming Basics in Objective-C > Using the Mutable Classes

Using the Mutable Classes

Up to this point, we've only discussed collection objects that are initialized once and can never change. While there are definitely places this is useful, what's even more useful is a collection class that can be modified. Each of the collection classes has a mutable version—we've only talked about the non-mutable classes. The classes are fundamentally the same except that elements can be added and removed from the mutable versions.

NSMutableSet

This can be initialized the same as the NSSet or can be initialized without any values and then values added. Consider the code in Listing 8–7:

Listing 8–7. Adding objects to an NSMutableSet.

 1  NSMutableSet *mySet = [[NSMutableSet alloc] init];
 2
 3  [mySet addObject:@"One"];
 4  [mySet addObject:@"Two"];
 5  [mySet addObject:@"Three"];
 6
 7  for (id val in mySet) {
 8      NSLog(@"%@", val);
 9  }

  

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