NSSet

 // 使用便利构造器创建集合
        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);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值