今天讲Objective-C的Set,无序不可以重复的集合。
Sets Are Unordered Collections
An NSSet is similar to an array, but maintains an unordered group of distinct objects, as shown in Figure 6-2.Figure 6-2 A Set of Objects
Because sets don’t maintain order, they offer a performance improvement over arrays when it comes to testing for membership.
The basic NSSet class is again immutable, so its contents must be specified at creation, using either allocation and initialization or a class factory method, like this:
NSSet *simpleSet =
[NSSet setWithObjects:@"Hello, World!", @42, aValue, anObject, nil];
As with NSArray, the initWithObjects: and setWithObjects: methods both take a nil-terminated, variable number of arguments. The mutable NSSet subclass is NSMutableSet.
Sets only store one reference to an individual object, even if you try and add an object more than once:
NSNumber *number = @42;
NSSet *numberSet =
[NSSet setWithObjects:number, number, number, number, nil];
// numberSet only contains one object
For more information on sets, see Sets: Unordered Collections of Objects.