Foundation框架 - NSSet类 、NSMutableSet类

NSSet和NSArray的对比

/*
 NSSet和NSArray的对比
 1> 共同点
 * 都是集合,都能存放多个OC对象
 * 只能存放OC对象,不能存放非OC对象类型(基本数据类型:int、char、float等,结构体,枚举)
 * 本身都不可变,都有一个可变的子类

 2> 不同点
 * NSArray有顺序,NSSet没有顺序
 */

重点内容

#import <Foundation/Foundation.h>
#import "Person.h"

static void ___log(NSSet* set1);
static void ___log(NSSet* set1)
{
    NSMutableString * str = [NSMutableString stringWithFormat:@"NSSet{"];

    for (id item in set1) {
        [str appendFormat:@"%@,",item];
    }
    //删除最后一个逗号
    NSRange rng ={str.length-1,1};
    [str deleteCharactersInRange:rng];
    [str appendString:@"}"];
    NSLog(@"%@",str);
}

演练set判断重复的依

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property NSString* name;
@property NSNumber* age;

@property (nonatomic, strong) NSString * week;
+ (instancetype) personWithName:(NSString*) name withAge:(NSNumber*)age;
@end
#import "Person.h"

@implementation Person

@synthesize name,age;

+ (instancetype)personWithName:(NSString *)name withAge:(NSNumber *)age
{
    Person* person=[[Person alloc]init];
    person.name=name;
    person.age=age;
    return person;
}

/**
 *  重写hash方法
 */
- (NSUInteger) hash
{
    NSLog(@"访问了hash");
    NSDate* date=[NSDate new];
    return date.timeIntervalSinceReferenceDate;
}

/**
 *  重写isEqual方法
 */
- (BOOL)isEqual:(id)object
{
    return YES;
}
@end
int main(int argc, const char * argv[]) {
    @autoreleasepool {

     //演练set判断重复的依据
        Person* jobs=[Person personWithName:@"steven jobs" withAge:[NSNumber numberWithInt:56]];
        Person* cook=[Person personWithName:@"david cook" withAge:[NSNumber numberWithInt:54]];
        Person* cook2=[Person personWithName:@"david cook" withAge:[NSNumber numberWithInt:54]];


        NSSet* personSet =[NSSet setWithObjects:jobs,cook,cook2, nil];
        NSLog(@"personSet_count:%lu",personSet.count);

集合的创建

        NSLog(@"*************************** 集合的创建 **************************");


        NSSet *s1 = [NSSet setWithObjects:@"tom",@"rose", @"jack",@"lily",nil];
        NSString *str =  [s1 anyObject];// 随机拿出一个元素
        NSLog(@"随机取出的元素为:%@", str);

        //集合里只能存放OC对象,不能存放非OC对象类型
        NSSet* set=[NSSet setWithObjects:@"rose",@"jack",[NSNumber numberWithInt:4],nil];
        NSLog(@"set的长度为:%lu",[set count]);
        for (id item in set) {
            NSLog(@"item:%@",item);
        }

集合的遍历

        NSLog(@"*************************** 集合的遍历 **************************");

        NSArray* arr=[NSArray arrayWithObjects:@"中国",@"日本", @"朝鲜",@"韩国",nil];
        NSSet* set1=[NSSet setWithArray:arr];
        NSLog(@"%2@",set1.className);

        NSArray* arr2=[set1 allObjects];
        for (NSString* item in arr2) {
            NSLog(@"%@",item);
        }

判断交集

        NSLog(@"**************************** 判断交集 ***************************");

        NSSet* intersect=[NSSet setWithObjects:@"中国",@"日本", nil];
        if ([set1 intersectsSet:intersect]) {
            NSLog(@"intersect是set1的交集");
        }

可变集合操作

NSLog(@"*************************** 可变集合操作 *************************");

        //从现有的set转换为MutableSet,获得一份可变集合的拷贝
        NSMutableSet* mset=[intersect mutableCopy];
        NSLog(@"mset:%@",mset.className);

        //添加对象
        [mset addObject:@"新加坡"];
        NSLog(@"添加对象后的集合为:%@",mset);

        //删除对象
        [mset removeObject:@"日本"];
        NSLog(@"删除对象后的集合为:%@",mset);

        //求并集
        [mset unionSet:set1];
        ___log(mset);
        NSLog(@"并集为:%@",mset);

        //求补集
        [mset minusSet:intersect];
        ___log(mset);
        NSLog(@"补集为:%@",mset);

        //求交集
        [mset intersectsSet:set1];
        ___log(mset);
        NSLog(@"交集为:%@",mset);

NSCountedSet

NSLog(@"*************************** NSCountedSet **************************");

        //计算字符串的重复次数  + (instancetype)orderedSet
        //The NSCountedSet class declares the programmatic interface to a mutable,
        //unordered collection of indistinct objects. A counted set is also known as a bag.
        NSString* sourStr=@"abc abc 123 ab cd cd";
        NSArray* strArr=[sourStr componentsSeparatedByString:@" "];
        NSCountedSet* csset=[NSCountedSet setWithArray:strArr];
        NSUInteger count=[csset countForObject:@"abc"];
        NSLog(@"求得的abc的重复数为:%lu",count);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值