OC: Collection class in OC-NSSet (2)
/*
NSSet set
In an NSSet object, a specific object can only appear once.
The greatest use of an NSSet object is to check whether an object exists. Fast
Comparison between NSSet and NSArray
Same: 1. All are used to store OC objects
2. Basic data types, struct, and enumeration cannot be directly stored.
3. They are both immutable, but all have a mutable subclass.
Difference: 1. NSArray is ordered, and NSSet is unordered.
2. The elements in NSSet are unique.
*/
// ================================================ ============================
// Create an empty set. No more elements can be added to it.
// NSSet * set1 = [NSSet set];
// Create a set with an element
NSSet * set2 = [NSSet setWithObject: @ "abc"];
// Create a set with Multiple Elements
Car * car1 = [Car new];
Car * car2 = [Car new];
NSSet * set3 = [NSSet setWithObjects: car1, car2, @ "abc", nil];
// Obtain the number of set Elements
NSUInteger count = set3.count;
[Set3 count];
NSLog (@ "% ld", count );
// Print the set (the printed element order is not fixed)
NSLog (@ "% @", set3 );
// Obtain all elements in the set. The returned value is NSArray.
NSArray * ary = [set3 allObjects];
// There is no order to retrieve an element from the set.
[Set3 anyObject];
// Determine whether an object is contained in the Set
[Set3 containsObject: @ "abc"]; // return BOOL type
If ([set3 containsObject: @ "abc"])
{
NSLog (@ "this object exists in set ");
}
Else
{
NSLog (@ "set does not contain this object ");
}
[Set3 member: @ "abc"]; // return BOOL type
If ([set3 member: @ "abc"])
{
NSLog (@ "this object exists in set ");
}
Else
{
NSLog (@ "set does not contain this object ");
}
// ================================================ ================================
// NSMutableSet variable set
// Initialize a variable set
NSMutableSet * set4 = [NSMutableSet set];
// Add Element
[Set4 addObject: [Car new];
// Add the elements in an array to the current set
NSArray * ary2 = @ [@ "jereh", @ "ios"];
[Set4 addObjectsFromArray: ary2];
NSLog (@ "% @", set4 );
// Delete. Because there is no order, you cannot delete the determined objects according to the index location.
// Delete an element
[Set4 removeObject: @ "ios"];
// Delete all elements
[Set4 removeAllObjects];
// Traverse the set (Use Quick traversal)
NSMutableSet * set5 = [NSMutableSet setWithObjects: @ "a", @ "B", @ "c", @ "d", nil];
NSArray * ary3 = @ [@ "jere"];
[Set5 addObjectsFromArray: ary2];
[Set5 addObjectsFromArray: ary3];
For (id obj in set5 ){
NSLog (@ "% @", obj );
}
// Add all elements of a set to the current set
NSLog (@ "% @", set4 );
NSLog (@ "% @", set5 );
NSMutableSet * set6 = [NSMutableSet setWithObject: @ "I love you."];
[Set6 unionSet: set5];
NSLog (@ "% @", set6 );
// Determine whether two sets are equal
If ([set5 isEqualToSet: set6] = YES ){
NSLog (@ "two sets are equal ");
}
Else
{
NSLog (@ "two sets are not equal ");
}
// Determine whether at least one element exists in the intersection of the two Sets
[Set6 intersectsSet: set5]; // BOOL
[Set6 intersectSet: set5]; // void
If ([set6 intersectsSet: set5] = YES ){
NSLog (@ "two sets have intersections ");
}
Else
{
NSLog (@ "there is no intersection between the two sets ");
}