原文链接
容器类常用的是数组字典,其实也能满足大多数需要,但偶然看到对集合的需要,觉得集合很实用。
先定义一个函数:把集合转换为NSString
NSString *NSCollectionToString(id collection) {
NSMutableString *result = [NSMutableString stringWithString:@"["];
for (id obj in collection) {
[result appendString:[obj description]];
[result appendString:@", "];
}
//如果collection是非空集合
if ([collection count] > 0) {
NSInteger len = [result length];//获取字符串的长度
//去掉最后的,和空格
[result deleteCharactersInRange:NSMakeRange(len -2, 2)];
[result appendString:@"]"];
return result;
}
//collection是空集合
else {
[result appendString:@"]"];
return result;
}
}
NSSet
//NSSet
- (void)aboutNSSet{
[super viewDidLoad];
//初始化NSSet,重复的元素不会被添加进去
NSSet *set1 = [NSSet setWithObjects:@"王二",@"张三",@"李四",@"老五",@"王二", nil];
NSLog(@"%ld",set1.count);
NSLog(@"%@",NSCollectionToString(set1));
//用集合初始化集合
NSSet *set2 = [NSSet setWithSet:[NSSet setWithObjects:@"老五",@"王二", nil]];
NSLog(@"%ld",set2.count);
NSLog(@"%@",NSCollectionToString(set2));
//拼接元素
set1 = [set1 setByAddingObject:@"wangEr"];
set1 = [set1 setByAddingObjectsFromSet:[NSSet setWithObjects:@"zhangSan",@"liSi", nil]];
set1 = [set1 setByAddingObjectsFromArray:@[@"wangWu"]];
NSLog(@"%@",NSCollectionToString(set1));
NSLog(@"%ld",set1.count);
//判断两个集合是否有交集
BOOL intersect = [set1 intersectsSet:set2];
BOOL equal = [set1 isEqualToSet:set2];
BOOL subset = [set1 isSubsetOfSet:set2];
NSLog(@"set2 是否包含 set1 %d",subset);
NSLog(@"两个集合是否相等%d",equal);
NSLog(@"两个集合是否有交集%d",intersect);
//判断集合是否包含某个元素
BOOL contain = [set1 containsObject:@"xiaowu"];
//判断是否包含某个元素,并返回该元素或nil
NSSet *s = [set1 member:@"wangEr"];
NSLog(@"set1 是否包含 xiaowu %d",contain);
NSLog(@"s 为 %@ ",s);
//返回集合中的同一个元素,下面两个结果一致,苹果不保证绝对随机
NSLog(@"set1 中的随机元素%@",[set1 anyObject]);
NSLog(@"set1 中的随机元素%@",[set1 anyObject]);
//对set 中所有元素进行block处理,返回所有长度大于3的组成新数组
NSSet *filterSet1 = [set1 objectsPassingTest:^BOOL(id _Nonnull obj, BOOL * _Nonnull stop) {
return (BOOL)([obj length] > 3);
}];
NSLog(@"set1中元素长度大于3的有%@",NSCollectionToString(filterSet1));
}
NSMutableSet
//NSMutableSet
- (void)aboutNSMutableSet{
//初始化
NSMutableSet *set = [NSMutableSet set];
//添加元素
[set addObject:@"wangEr"];
[set addObjectsFromArray:@[@"zhangSan",@"liSi",@"wangWu"]];
NSLog(@"添加元素后集合%@",NSCollectionToString(set));
//从NSMutableSet中删除元素
[set removeObject:@"wangEr"];
NSLog(@"删除元素后的集合%@",NSCollectionToString(set));
[set removeAllObjects];
NSLog(@"清空后的集合%@",NSCollectionToString(set));
//用新集合替换旧集合
[set setSet:[NSSet setWithObjects:@"王二",@"张三", nil]];
NSSet *set2 = [NSSet setWithObjects:@"李四",@"老五",@"王二", nil];
//并集,交集,差集,调用者为NSMutableSet,但对数可为NSMutableSet/NSSet
[set unionSet:set2];//并集
NSLog(@"并集为%@",NSCollectionToString(set));
[set minusSet:set2];//差集
NSLog(@"差集为%@",NSCollectionToString(set));
[set intersectSet:set2];//交集
NSLog(@"交集为%@",NSCollectionToString(set));
}
NSCountSet
- (void)aboutNSCountSet{
//可计数集合继承自NSMutableSet,所以它可以修改
NSCountedSet *set = [NSCountedSet set];
[set addObject:@"wangEr"];
[set addObjectsFromArray:@[@"wangEr",@"zhangSan",@"liSi",@"laoWu"]];
NSLog(@"%@",NSCollectionToString(set));
NSLog(@"%ld",set.count); //4个元素
//获取元素出现的次数
NSLog(@"wangEr 出再的次数是%ld",[set countForObject:@"wangEr"]);
//删除元素
[set removeObject:@"wangEr"];
NSLog(@"删除 1 次 wangEr 后的集合是%@",NSCollectionToString(set));
NSLog(@"删除 1 次 wangEr 交数是%ld",[set countForObject:@"wangEr"]);
[set removeObject:@"wangEr"];
NSLog(@"删除 2 次 wangEr 后的集合是%@",NSCollectionToString(set));
NSLog(@"删除 2 次 wangEr 交数是%ld",[set countForObject:@"wangEr"]);
}
NSIndexSet
NSMutableArray *bookArray=[NSMutableArray array];
Book *book1=[Book bookWithName:@"chinese" price:19];
Book *book2=[Book bookWithName:@"math" price:16];
Book *book3=[Book bookWithName:@"english" price:15];
[bookArray addObject:book1];
[bookArray addObject:book2];
[bookArray addObject:book3];
//检测判断,返回第一个符合条件的数据索引
NSUInteger pass = [bookArray indexOfObjectPassingTest:^BOOL(Book *obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.price > 25) {
return YES;
}else{
return NO;
}
}];
if (pass == NSNotFound) {
NSLog(@"notFound");
}
//检测判断,返回符合条件的数据索引集合
NSIndexSet *testSet = [bookArray indexesOfObjectsPassingTest:^BOOL(Book* obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.price> 15) {
return YES;
}else{
return NO;
}
}];
NSLog(@"%ld",testSet.count);
[testSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"======%lu",idx);
}];
NSOrderedSet
NSOrderSet 继承自 NSObject
NSMutableOrderSet 继承自 NSOrderSet
NSOrderedSet具有NSSet相似的功能,而NSMutableOrderedSet具有NSMutableSet相似的功能,
有增加、删除、替换元素,排序,计算集合的交集、并集、差集等功能,同时有根据索引来对元素进行操作的功能
- (void)aboutOrderedSet{
NSOrderedSet *orderSet = [NSOrderedSet orderedSetWithObjects:@"wangEr",@"zhangSan",@"liSi",@"laoWu", nil];
NSLog(@"orderSet为%@",NSCollectionToString(orderSet));//有序
//根据索引进行访问
NSLog(@"第一个元素是%@",[orderSet firstObject]);
NSLog(@"第二个元素是%@",[orderSet objectAtIndex:1]);
NSLog(@"最后一个元素是%@",[orderSet lastObject]);
NSLog(@"(0-2)范围内的元素是%@",NSCollectionToString( [orderSet objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2) ]]));
//用block返因长度 > 4 的元素索引
NSIndexSet *indexSet = [orderSet indexesOfObjectsPassingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
return (BOOL)([obj length]>4);
}];
NSLog(@"长度大于4的元素索引为%@",indexSet);
//可变有序集合
NSMutableOrderedSet *mutableOrderSet = [NSMutableOrderedSet orderedSet];
[mutableOrderSet addObject:@"wangEr"];
[mutableOrderSet addObjectsFromArray:[NSArray arrayWithObjects:@"wangEr",@"zhangSan",@"liSi",@"laoWu",nil]];
//根据索引删除元素
[mutableOrderSet removeObjectAtIndex:2];
NSLog(@"删除第二个元素后的集合是%@",NSCollectionToString(mutableOrderSet));
[mutableOrderSet removeObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)]];
NSLog(@"删除第一个元素后的集合是%@",NSCollectionToString(mutableOrderSet));
[mutableOrderSet removeObjectsInRange:NSMakeRange(0, 2)];
NSLog(@"删除两个元素后的集合是%@",NSCollectionToString(mutableOrderSet));
}
NSSet如何判断两个元素是否相等?
同时满足两个条件
1.两个对象通过isEqual:方法返回YES,即指针或内容相等
2.两个对象的hash方法返回值相等,即hashCode相等
但两个对象内容相等,但它们的hashCode不一定相等,
所以重写父类的两个方法
-(BOOL)isEqual:(id)object 和 - (NSInteger) hash
//不重写父类方法的情况下系统会认为添加了5个不同的元素
NSSet *set = [NSSet setWithObjects:
[[User alloc] initWithName:@"sun" pass:@"123"],
[[User alloc] initWithName:@"bai" pass:@"234"],
[[User alloc] initWithName:@"sun" pass:@"123"],
[[User alloc] initWithName:@"hu" pass:@"456"],
[[User alloc] initWithName:@"gan" pass:@"567"],
nil];
重写继承自NSObject的isEqual:
- (BOOL) isEqual:(id)object {
if (self == object) {
return YES;
}
if ([object class] == [self class]) {
User *target = (User *) object;
return [self.name isEqualToString:target.name] && [self.pass isEqualToString:target.pass];
}
return NO;
}
重写继承自NSObject的hash方法
- (NSInteger) hash {
NSInteger nameHash = (self.name == nil ? 0 : [self.name hash]);
NSInteger passHash = (self.pass == nil ? 0 : [self.pass hash]);
return nameHash * 31 + passHash;
}