NSSet

来源:互联网 发布:0--100水仙花数java 编辑:程序博客网 时间:2024/05/18 22:45
 // 使用便利构造器创建集合        NSSet *set1 = [NSSet setWithObjects:@"qqq", @"www", @"qqq", nil];        NSLog(@"%@", set1);        // 使用数组创建集合        NSArray *arr1 = [NSArray arrayWithObjects:@"12", @"23", @"12", nil];        NSSet *set2 = [NSSet setWithArray:arr1];        NSLog(@"%@", set2);        // 打印集合元素个数        NSLog(@"set2 count = %ld", [set2 count]);        // 创建空的可变集合//        NSMutableSet *mutableSet = [NSMutableSet set];        NSMutableSet *mutableSet1 = [NSMutableSet setWithSet:nil];        // 添加两个不同元素        [mutableSet1 addObject:@"55"];        [mutableSet1 addObject:@"45"];        [mutableSet1 addObject:@"55"];        [mutableSet1 addObjectsFromArray:@[@"ww", @"ww"]];        NSLog(@"%@", mutableSet1);        // 删除元素        [mutableSet1 removeObject:@"45"];        NSLog(@"%@", mutableSet1);        //NSCountedSet        // 通过集合计算重复元素的个数        NSArray *ages = @[@13, @(23), @23, @12, @12, @12, @34, @345, @34, @23, @23];        NSCountedSet *agesSet = [NSCountedSet setWithArray:ages];        // 获取重复对象的个数        NSUInteger count = [agesSet count];        NSLog(@"count = %lu", count);        NSUInteger count1 = [agesSet countForObject:@23];        NSLog(@"23 = %lu", count1);        NSUInteger count2 = [agesSet countForObject:@12];        NSLog(@"12 = %lu", count2);        // 快速遍历        for (NSNumber *number in ages) {            NSLog(@"%@", number);        }
0 0