首页 > 代码库 > 集合-字典 方法练习

集合-字典 方法练习

- (void)viewDidLoad {

    [super viewDidLoad];

     //用一个或多个键值对初始化一个字典对象,以值,键,值,键,值,键,...,nil的顺序

    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@1,@"key1",@2,@"key2",@3,@"key3" ,nil];

    NSLog(@"dict:%@",dict);

    //用两个数组初始化一个字典(一个key数组,一个value数组,大小必须相等,否则崩溃

    NSArray *valueArray = @[@"1",@"2",@"3"];

    NSArray *keyArray = @[@"key1",@"key2",@"key"];

    NSDictionary *dict2 = [[NSDictionary alloc] initWithObjects:valueArray forKeys:keyArray];

    NSLog(@"dict:%@",dict2);

    NSDictionary *dict3 = [[NSDictionary alloc] initWithDictionary:dict];

    NSLog(@"dict:%@",dict3);

    

    //保存字典对象内容到文件

    NSString *path = @"/Users/apple/Desktop/dict.plist";

    [dict writeToFile:path atomically:YES];

    //从之前保存的文件读取数据到字典对象

    NSDictionary *dict4 = [NSDictionary dictionaryWithContentsOfFile:path];

    NSLog(@"dict4:%@",dict4);

    //不可变字典一样可以字面常量初始化,key在前,value在后

    NSDictionary *dict5 = @{@"key":@1,@"key1":@2,@"key3":@3};

    NSLog(@"dict5:%@",dict5);

    //通过指定的key来取对应的的对象,如果没有这个key,返回nil

    NSNumber *num = [dict objectForKey:@"key"];

    NSLog(@"num:%@",num);

    //获取字典中键值对的个数

    NSInteger pairCount = [dict count];

    NSLog(@"pairCount:%d",pairCount);

    NSArray *allkeys = [dict allKeys];

    NSArray *allvalues = [dict allValues];

    NSLog(@"allkeys:%@",allkeys);

    NSLog(@"allvalue:%@",allvalues);

   

    //老办法遍历

    NSArray *allkeys2 = [dict2 allKeys];

    NSUInteger count = [allkeys2 count];

    for (int i=0; i<count; i++) {

        id key = [allkeys2 objectAtIndex:i];

        id value = http://www.mamicode.com/[dict2 objectForKey:key];

        NSLog(@"%@:%@",key,value);

    }

    

    //快速枚举

    for(id key in dict2){

        id value = http://www.mamicode.com/[dict objectForKey:key];

        NSLog(@"%@:%@",key,value);

    }

    

    //不可变字典对象

    NSMutableDictionary *mdict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@1,@"key",@2,@"key2", nil];

    //删除一个键-值

    [mdict removeObjectForKey:@"key"];

    NSLog(@"mdict:%@",mdict);

    //添加一个键-值

    [mdict setObject:@3 forKey:@"key3"];

    NSLog(@"mdict:%@",mdict);

    NSMutableDictionary *mdict2 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@1,@"key4",@2,@"key4",@3,@"key5", nil];

     //将一个字典对象里的键-值添加到另一个字典对象里

    [mdict2 addEntriesFromDictionary:mdict];

    NSLog(@"mdcit2:%@",mdict2);

    [mdict removeAllObjects];

    NSLog(@"mdict:%@",mdict);

    

    NSMutableDictionary *mdict3 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@1,@"key3",@2,@"key4",@3,@"key5", nil];

    NSMutableDictionary *mdict4 =[[NSMutableDictionary alloc] initWithObjectsAndKeys:nil];

    [mdict4 setDictionary:mdict3];

    NSLog(@"mdict4:%@",mdict4);

    

    NSArray *array = @[@"key4",@2,@"key4",@3,@"key5"];

    [mdict3 removeObjectsForKeys:array];

    //集合

    NSSet *set = [[NSSet alloc] initWithObjects:@1,@2,@3, nil];

    NSSet *set2 = [[NSSet alloc] initWithObjects:@3,@4,@5, nil];

    //随机取一个元素

    NSNumber *num2 = [set anyObject];

    NSLog(@"num2:%@",num2);

    //判断集合是否包含某元素

    BOOL b = [set containsObject:@1];

    NSLog(@"%hhd",b);

    //判断两个集合是否有交集

    BOOL a = [set intersectsSet:set2];

     NSLog(@"%hhd",a);

    //判断两集合是否相等

    BOOL c = [set isEqualToSet:set];

     NSLog(@"%hhd",c);

    //判断一个集合是否是两一个集合的子集

    BOOL d = [set isSubsetOfSet:set2];

     NSLog(@"%hhd",d);

    

    //不可变集合

    NSMutableSet *mset = [[NSMutableSet alloc] initWithObjects:@1,@2,@3, nil];

    NSMutableSet *mset2 = [[NSMutableSet alloc] initWithObjects:@2,@3, nil];

   

   //[mset intersectsSet:mset2];

    //[mset addObject:@4];

    //[mset removeObject:@4];

    //NSArray *array3 = @[@4,@5];

    //[mset addObjectsFromArray:array3];

    //[mset minusSet:mset2];

   // [mset removeAllObjects];

    [mset unionSet:mset2];

    NSLog(@"%@",mset);

    

 

    

 

}