object-c学习笔记:Foundation框架常用类(二)
四、NSDictionary和NSMutableDictionary类
//____________________NSSet________________________ // 集合的创建 NSSet *set1 = [NSSet setWithObjects:@"1", @"2", nil]; NSLog(@"set1 : %@", set1); NSSet *set2 = [[NSSet alloc] initWithObjects:@"3", @"4", @"2", nil]; NSLog(@"set2 : %@", set2); NSArray *array = [NSArray arrayWithObjects:@"8", @"9", nil]; NSSet *set3 = [NSSet setWithArray:array]; NSLog(@"set3 : %@", set3); NSSet *set4 = [NSSet setWithSet:set1]; NSLog(@"set4 : %@", set4); //取集合的元素个数 int count = [set2 count]; NSLog(@"count : %d", count); // 将集合入一个数组中 NSArray *array2 = [set2 allObjects]; NSLog(@"array2 : %@", array2); // 取任意一个元素 id object = [set2 anyObject]; NSLog(@"object : %@", object); // 是否包含某个对象 BOOL isContain = [set2 containsObject:@"2"]; NSLog(@"isContain : %d", isContain); // 判断两个set是否存在交集 BOOL isInter = [set1 intersectsSet:set2]; NSLog(@"isInter : %d", isInter); // 判断两个集合中的元素是否完全相同 BOOL isEqual = [set1 isEqualToSet:set2]; NSLog(@"isEqual : %d", isEqual); // 判断一个集合是否是另一个集合的子集 BOOL isSub = [set1 isSubsetOfSet:set2]; NSLog(@"isSub : %d", isSub); // 通过已有的两个集合创建一个新的集合 NSSet *set7 = [NSSet setWithObjects:@"2", nil]; NSSet *set8 = [NSSet setWithObjects:@"3", nil]; NSSet *set9 = [set7 setByAddingObjectsFromSet:set8]; NSLog(@"set9 : %@", set9); ////////////////////////////////// // NSMutableSet 继承自NSSet // 创建可变集合 NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"2", @"1", nil]; NSMutableSet *mutableSet2 = [NSMutableSet setWithObjects:@"3", @"1", nil]; // 集合1减去集合2 [mutableSet1 minusSet:mutableSet2]; NSLog(@"mutableSet1 : %@", mutableSet1); // 集合2和集合1的交集 [mutableSet1 intersectSet:mutableSet2]; NSLog(@"mutableSet1 : %@", mutableSet1); // 并集 [mutableSet1 unionSet:mutableSet2]; NSLog(@"mutableSet1 : %@", mutableSet1); // 将集合一设置为集合二的内容 [mutableSet1 setSet:mutableSet2]; NSLog(@"mutableSet1 : %@", mutableSet1); // 删除指定的对象 [mutableSet1 removeObject:@"1"]; NSLog(@"mutableSet1 : %@", mutableSet1);