字典集合

来源:互联网 发布:mysql trigger if 编辑:程序博客网 时间:2024/05/03 15:58


//一,字典--NSDictionary   key键找到唯一的value 值
        
//      特点:1,只能存对象
//           2,字典是无序的
//           3,key 值不可以重复  (如果重复,保留前面的键值对)
//           4, value 值 可以重复
//           5, key 值 和 value 值 必须是对象
//           6, 一般情况下,key值只使用字符串
//        
//         NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:@"小明",@"小明对应男生",
//                                                                         @"小翠",@"小翠对应女生", nil];
//        NSLog(@"%@",dict);
//        NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"小明",@"对应哥哥",
//                                                                         @"小翠",@"对应妹妹", nil];
//        NSLog(@"%@",dict1);
//        
//        //只能写一个键值对,不常用
//        NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
//        NSLog(@"%@",dict2);
//        //语法糖
//        NSDictionary *dict3 = @{@"key1": @"value1",
//                                @"key2": @"value2",
//                                @"key3": @"value3",
//                                @"key4": @"value2"};
//        NSLog(@"%@",dict3);
//        
//        
//        
////字典的操作方法  (前面四个很重要)
////      1,cout
//        NSUInteger dictCount = [dict3 count];
//        NSLog(@"%lu",dictCount);
////      2, 拿出字典所有的 key 值
//        NSArray *keys = [dict3 allKeys];
//        NSLog(@"dict3里面的所有的 key 为 %@",keys);
////      3, 拿出字典里面所有的 value 值
//        NSArray *values = [dict3 allValues];
//        NSLog(@"dict3里面的所有的 value 为 %@",values);
//
//        Person *person1 = [[Person alloc]initWithName:@"gxm"];
//        Person *person2 = [[Person alloc]initWithName:@"lh"];
//        NSDictionary *finalDic = @{@"key1": person1,@"key2":person2};
//        NSLog(@"%@",finalDic);
////      4, 拿出某一个 value 值
//        [dict3 objectForKey:@"key1"];
//        // 语法糖去实现
//         NSString *value = dict3[@"key2"];
//         NSLog(@"aaaa%@",value);
//        NSLog(@"%@",dict3[@"key2"]);
////      5, 根据 value 拿出某些个 key 值(这个方法不常用)
//        NSArray *keysArray = [dict3 allKeysForObject:@"value2"];
//        NSLog(@"bbbb%@",keysArray);
//      //6,遍历字典
////        for (id object in dict3) {  //打印出 key 值
////            NSLog(@"12%@",object);
////        }
////        for (id object in dict3) {   //通过 key 值找出 value 值
////            NSLog(@"21%@",dict3[object]);
////        }
//        NSArray *keysArray1 = [dict3 allKeys];
//        for (NSString *key in keysArray1) {
//            NSLog(@"11111%@",key);
//        }
//    NSLog(@"1111%@",keysArray1);
//        
////  字典的其他使用
////        1,数组和字典可以相互嵌套
////        2,如果字典里面放数组,一般情况把数组作为 value 值
////        
//
//        
////二,  NSMutableDictionary  可变的字典 -----------继承不可变的字典
//        // 特点:key 值如果重复,保留后面的键值对
//        //NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc]initWithCapacity:10];
//        NSMutableDictionary *mutableDic1 = [NSMutableDictionary dictionary];
//        //NSLog(@"%@",mutableDic);
////       1,往可变的字典里面去添加元素
//        [mutableDic1 setObject:person1 forKey:@"key1"];
//        [mutableDic1 setObject:person2 forKey:@"key2"];
//        [mutableDic1 setObject:@"value" forKey:@"key1"];
//        NSLog(@"AAAA%@",mutableDic1);
////       2,删除字典里面的元素
//// 删除字典里面的所有元素
//        //[mutableDic1 removeAllObjects];
//        //NSLog(@"%@",mutableDic1);
//// 删除某一个值
//        [mutableDic1 removeObjectForKey:@"key1"];
//        NSLog(@"%@",mutableDic1);
////
//        
//        NSArray *deleteArray = @[@"key1",@"key2"];
//        [mutableDic1 removeObjectsForKeys:deleteArray];
//

        
        
//三 NSSet  集合
//   1,里面的只可以存对象
//   2,里面的元素不能重复
//   3,集合是无序的
//
//
//        NSSet *set = [NSSet setWithObjects:@"ni",@"hao",@"shuai",@"2", nil];
//        NSLog(@"%@",set);
//        
////   把数组里面的元素放到 set 里面 (以下三行代码用来去除重复元素)
//        NSArray *array = @[@"1",@"2",@"1"];
//        NSSet *set1 = [NSSet setWithArray:array];
//        NSLog(@"%@",set1);
//        
////  操作 NSSet 的方法
////   1,取出集合里面所有的元素
//        NSLog(@"....%@",set1.allObjects);
//        NSLog(@",,,,%lu",set1.count); //统计set中元素的个数
////   2,取出集合里面某个元素 不保证随机性
//        NSLog(@"----%@",[set1 anyObject]);
//        
////   3,交集
//    
//        NSLog(@";;;;%d", [set1 intersectsSet:set]);
//        
//        
//四 NSMutableSet  可变的集合
        NSMutableSet *set2 = [NSMutableSet setWithObjects:@"3",@"2",@"4", nil];
        NSMutableSet *set3 = [NSMutableSet setWithObjects:@"1",@"4",@"5", nil];
        
////交集
//        [set2 intersectSet:set3];
//        NSLog(@"%@", set2);
////并集
//        [set2 unionSet:set3];
//        NSLog(@"%@", set2);
//差
        [set2 minusSet:set3];
        NSLog(@"%@", set2);
//
////五    NSCountedSet 集合计数
//        
//        NSCountedSet *countSet = [NSCountedSet setWithObjects:@"1",@"3",@"1",@"6", nil];  //两个1,一个3,一个6
//        NSLog(@"%@", countSet);
//


0 0
原创粉丝点击