main
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //NSSet 集合对象 //NSSet 使用散列表 hash table 形式 的技术存储对象 便于对象查找 在查找对象时一次命中 NSArray* array =[NSArray arrayWithObjects:@1,@2,@3, nil]; NSSet* set =[NSSet setWithObjects:@1,@2,@3,nil]; NSSet* set2 =[NSSet setWithArray:array]; [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog(@"idx=%lu,obj=%@",idx,obj); }]; // 区别 set里面没有索引 array含有索引。 输出set 没有顺序 [set enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) { NSLog(@"obj=%@",obj); }]; //NSMutableSet 可变的集合 //NSSet 中不能存储重复的对象 NSMutableSet* set3=[[NSMutableSet alloc]init]; [set3 addObject:@1]; [set3 addObject:@2]; [set3 addObject:@3]; // [set3 addObject:@2]; [set3 removeObject:@2]; [set3 enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) { NSLog(@"obj=%@",obj); }]; } return 0; }
main
int main(int argc, const char * argv[]) { @autoreleasepool { //NSSet 集合对象 //NSSet 使用散列表 hash table 形式 的技术存储对象 便于对象查找 在查找对象时一次命中 NSArray* array =[NSArray arrayWithObjects:@1,@2,@3, nil]; NSSet* set =[NSSet setWithObjects:@1,@3,nil]; NSSet* set2 =[NSSet setWithArray:array]; [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog(@"idx=%lu,obj=%@",idx,obj); }]; // 区别 set里面没有索引 array含有索引。 输出set 没有顺序 [set enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) { NSLog(@"obj=%@",obj); }]; //NSMutableSet 可变的集合 //NSSet 中不能存储重复的对象 NSMutableSet* set3=[[NSMutableSet alloc]init]; [set3 addObject:@1]; [set3 addObject:@2]; [set3 addObject:@3]; // [set3 addObject:@2]; // [set3 removeObject:@2]; [set3 enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) { NSLog(@"obj=%@",obj); }]; //可以判断一个对象是否在集合中 if([set3 containsObject:@2]) { NSLog(@"@2在set3中"); } //一个对象是否在集合中 id obj4 =[set3 member:@1]; NSLog(@"%@",obj4); //集合间的运算 集合做减法 将两个相同的元素都去掉 [set3 minusSet:set]; NSLog(@"%@",set3); //集合的合并 [set3 unionSet:set]; NSLog(@"%@",set3); //集合交集 [set3 intersectSet:set]; NSLog(@"%@,",set3); // 判断一个集合是否为另一个集合的子集 if([set3 isSubsetOfSet:set]) { NSLog(@"set 是 set3 的子集"); } } return 0; }