Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
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.
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 }