今天突然发现自己对NSMutableSet不是很了解,于是学习了一番。
1. NSMutableSet是集合 与NSMutableArray NSMutableDictionary 一样,都不能直接存储C的对象 只能转换为oc对象才可以 如
NSInteger NSString .
2. NSMutableSet 集合 乱序 在nsmutableset中得对象是乱序的 而且是对象不可以重复的
NSArray * array =@[@"1",@"2",@"3",@"4",@"5",@"5"];
NSMutableSet * set =[[NSMutableSet alloc]initWithArray:array];
NSLog(@"set=%@",set);
//结果
// set={(
// 3,
// 1,
// 4,
// 2,
// 5
// )} 可见对象是不能重复的 会自动滤重
3 比较某个对象是否在NSMutableSet 中
使用方法:
- (BOOL)containsObject:(id)anObject;
yes 存在 no不存在
4 俩个NSMutableSet对象的并集
方法
- (void)unionSet:(NSSet *)otherSet;
实现
NSArray * array =@[@"1",@"2",@"3",@"4",@"5",@"5"];
NSArray * array1 =@[@"1",@"3",@"6",@"5"];
NSMutableSet * set =[[NSMutableSet alloc]initWithArray:array];
NSMutableSet * set1 =[[NSMutableSet alloc]initWithArray:array1];
[set unionSet:set1];
NSLog(@"set=%@",set);
// set={(
// 3,
// 1,
// 6,
// 4,
// 2,
// 5
// )} 结果是俩个对象的并集
- (void)intersectSet:(NSSet *)otherSet;
NSArray * array =@[@"1",@"2",@"3",@"4",@"5",@"5"];
NSArray * array1 =@[@"1",@"3",@"6",@"5"];
NSMutableSet * set =[[NSMutableSet alloc]initWithArray:array];
NSMutableSet * set1 =[[NSMutableSet alloc]initWithArray:array1];
[set intersectSet:set1];
NSLog(@"set=%@",set);
// set={(
// 3,
// 1,
// 5
// )} 结果是俩个对象的交集
6 俩个对象的差集
方法:
- (void)minusSet:(NSSet *)otherSet;
NSArray * array =@[@"1",@"2",@"3",@"4",@"5",@"5"];
NSArray * array1 =@[@"1",@"3",@"6",@"5"];
NSMutableSet * set =[[NSMutableSet alloc]initWithArray:array];
NSMutableSet * set1 =[[NSMutableSet alloc]initWithArray:array1];
[set minusSet:set1];
// [set intersectSet:set1];
NSLog(@"set=%@",set);
// set={(
// 4,
// 2
// )} 结果为set1 中没有的对象