1.创建
- (void)removeDistinct
{
NSOrderedSet *set1 = [NSOrderedSet orderedSetWithArray:@[@3,@7,@3,@3,@0]];
NSLog(@"%@",set1);
NSArray *array = @[@3];
NSOrderedSet *set2 = [NSOrderedSet orderedSetWithArray:array range:NSMakeRange(0, 1) copyItems:NO];
NSLog(@"%@",set2);
NSOrderedSet *set3 = [NSOrderedSet orderedSetWithObjects:@"aaaa",@4, nil];
NSLog(@"%@",set3);
}
2.查找及遍历
- (void)queryElement
{
NSOrderedSet *set1 = [NSOrderedSet orderedSetWithArray:@[@3,@7,@3,@3,@0]];
NSLog(@"%@",set1);
if ([set1 containsObject:@3]) {
NSLog(@"EXIST");
}
[set1 enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@",obj);
}];
}
3.集合运算
- (void)collection
{
NSOrderedSet *set1 = [NSOrderedSet orderedSetWithObjects:@4,@5,@6,@7,nil];
NSMutableOrderedSet *set2 = [NSMutableOrderedSet orderedSetWithObjects:@5,@7,@9, nil];
if ([set1 intersectsOrderedSet:set2]) {
NSLog(@"intersectsOrderedSet -- yes");
}
if ([set2 isSubsetOfOrderedSet:set1]) {
NSLog(@"isSubsetOfOrderedSet -- yes");
}
[set2 unionOrderedSet:set1];
NSLog(@"%@",set2);
}
过滤元素
- (void)queryIndexes
{
NSOrderedSet *set = [NSOrderedSet orderedSetWithObjects:@3,@1,@8,@6,@12, nil];
NSIndexSet *indexs = [set indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj integerValue] >= 6) {
return YES;
}
return NO;
}];
NSArray *valueArray = [set objectsAtIndexes:indexs];
NSLog(@"%@",valueArray);
}
输出:8,6,12