字典集合


//一,字典--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);
//


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章