Ios notes-NSSet
//// Main. m // NSSet /// Created by Passer-by on 15-3-23. // Copyright (c) 2015 MAC. all rights reserved. // # import
Int main (int argc, const char * argv []) {@ autoreleasepool {/* comparison between NSSet and NSArray 1> commonalities * are set, multiple OC objects can be stored. * Only OC objects can be stored. Non-OC object types (basic data types: int, char, float, struct, and enumeration) cannot be stored, each has a variable subclass 2> difference * NSArray has a sequence, NSSet has no sequence */NSSet * set1 = [NSSet setWithObjects: @ "a", @ "B ", @ "c", @ "d", nil]; NSSet * set2 = [[NSSet alloc] initWithObjects: @ "1", @ "2", @ "3 ", nil]; NSArray * array = [NSArray arrayWithObjects: @ "a", @ "B", @ "c", nil]; NSSet * set3 = [NSSet setWithArray: array]; NSLog (@ "set1: % @", set1); NSLog (@ "set2: % @", set2); NSLog (@ "set3: % @", set3 ); // obtain the number of sets NSLog (@ "set1 count: % d", set1.count); // obtain all objects in the set in the form of arrays NSArray * allObj = [set2 allObjects]; NSLog (@ "allObj: % @", allObj); // obtain any object NSLog (@ "anyObj: % @", [set3 anyObject]); // whether an object NSLog is contained (@ "contains: % d", [set3 containsObject: @ "obj2"]); // whether the NSLog object in the specified set is included (@ "intersect obj: % d", [set1 intersectsSet: set3]); // whether the NSLog (@ "isEqual: % d ", [set2 isEqualToSet: set3]); // whether it is a subset NSLog (@" isSubSet: % d ", [set3 isSubsetOfSet: set1]); NSSet * set4 = [NSSet setWithObjects: @ "a", @ "B", nil]; NSArray * ary = [NSArray arrayWithObjects: @ "1", @ "2 ", @ "3", @ "4", nil]; NSSet * set5 = [set4 setByAddingObjectsFromArray: ary]; NSLog (@ "addFromArray: % @", set5 ); NSMutableSet * mutableSet1 = [NSMutableSet parameters: @ "1", @ "2", @ "3", nil]; NSMutableSet * mutableSet2 = [NSMutableSet setWithObjects: @ "", @ "2", @ "B", nil]; NSMutableSet * mutableSet3 = [NSMutableSet setWithObjects: @ "1", @ "c", @ "B", nil]; // The set element subtraction [mutableSet1 minusSet: mutableSet2]; NSLog (@ "minus: % @", mutableSet1); // only the equivalent element is left [mutableSet1 intersectSet: mutableSet3]; NSLog (@ "intersect: % @", mutableSet1); // merge the set [mutableSet2 unionSet: mutableSet3]; NSLog (@ "union: % @", mutableSet2 ); // Delete the specified Element [mutableSet2 removeObject: @ "a"]; NSLog (@ "removeObj: % @", mutableSet2); // delete all data [mutableSet2 removeAllObjects]; NSLog (@ "removeAll: % @", mutableSet2);} return 0 ;}