zoukankan      html  css  js  c++  java
  • NSDictionary 使用总结

    NSDictionary使用小结

     

    [plain] view plaincopy
    1. #import <Foundation/Foundation.h>  
    2.   
    3.   
    4. int main(int argc, const char * argv[])  
    5. {  
    6.   
    7.     @autoreleasepool {  
    8.   
    9.         //创建字典  
    10.         NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];  
    11.         NSLog(@"dic1 :%@", dic1);  
    12.           
    13.           
    14.         //创建多个字典  
    15.         NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:  
    16.                               @"value1", @"key1",  
    17.                               @"value2", @"key2",  
    18.                               @"value3", @"key3",  
    19.                               @"value4", @"key4",  
    20.                               nil];  
    21.         NSLog(@"dic2 :%@", dic2);  
    22.           
    23.           
    24.         //根据现有的字典创建字典  
    25.         NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];  
    26.         NSLog(@"dic3 :%@", dic3);  
    27.           
    28.           
    29.         //根据key获取value  
    30.         NSLog(@"key3 value :%@", [dic3 objectForKey:@"key3"]);  
    31.           
    32.         //获取字典数量  
    33.         NSLog(@"dic count :%d", dic3.count);  
    34.           
    35.         //所有的键集合  
    36.         NSArray *keys = [dic3 allKeys];  
    37.         NSLog(@"keys :%@", keys);  
    38.           
    39.         //所有值集合  
    40.         NSArray *values = [dic3 allValues];  
    41.         NSLog(@"values :%@", values);  
    42.           
    43.           
    44.           
    45.         NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:  
    46.                                            @"mvalue1", @"mkey1",  
    47.                                            @"mvalue2", @"mkey2", nil];  
    48.         //添加现有的字典数据  
    49.         [mutableDic addEntriesFromDictionary:dic3];  
    50.         NSLog(@"mutableDic :%@",mutableDic);  
    51.           
    52.         //添加新的键值对象  
    53.         [mutableDic setValue:@"set1" forKey:@"setKey1"];  
    54.         NSLog(@"set value for key :%@",mutableDic);  
    55.           
    56.         //以新的字典数据覆盖旧的字典数据  
    57.         [mutableDic setDictionary:dic2];  
    58.         NSLog(@" set dictionary :%@",mutableDic);  
    59.           
    60.         //根据key删除value  
    61.         [mutableDic removeObjectForKey:@"key1"];  
    62.         NSLog(@"removeForkey :%@",mutableDic);  
    63.           
    64.         //快速遍历  
    65.         for(id key in mutableDic) {  
    66.             NSLog(@"key :%@  value :%@", key, [mutableDic objectForKey:key]);  
    67.         }  
    68.           
    69.         //枚举遍历  
    70.         NSEnumerator *enumerator = [mutableDic keyEnumerator];  
    71.         id key = [enumerator nextObject];  
    72.         while (key) {  
    73.             NSLog(@"enumerator :%@", [mutableDic objectForKey:key]);  
    74.             key = [enumerator nextObject];  
    75.         }  
    76.           
    77.           
    78.         //根据key数组删除元素  
    79.         [mutableDic removeObjectsForKeys:keys];  
    80.         NSLog(@"removeObjectsForKeys :%@",mutableDic);  
    81.           
    82.         [mutableDic removeAllObjects];  
    83.         //删除所有元素  
    84.         NSLog(@"remove all :%@", mutableDic);  
    85.     }  
    86.     return 0;  
    87. }  

     

    日志:

     

     

    NSDictionary 使用总结

     
     
    1. NSArray *m_array = [NSArray arrayWithObjects:@"first",@"second",nil];  
    2. NSArray *n_array = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];  
    3. //使用类方法初始化,系统自动释放内存  
    4. NSDictionary *test_dictionary = [NSDictionary dictionaryWithObjectsAndKeys:m_array,@"sort",n_array,@"number",nil];  
     
    NSLog(@"myDictionary = %@",myDictionary);
    1. //获取字典包含对象数目  
    2. int dict_size = [test_dictionary count];  
    3. //访问字典中的值  
    4. NSArray *sort_array = [test_dictionary objectForKey:@"sort"];  
    5. //获取键值  
    6. NSArray *keys = [test_dictionary allKeysForObject:sort_array];  
    7. //获取字典中所有值,数组  
    8. NSArray *all_value = [test_dictionary allValues];  
    9. //快速枚举  
    10. for(id key in test_dictionary)  
    11. {  
    12.     NSLog(@"key: %@,value: %@",key,[test_dictionary objectForKey:key]);  
    13. }  
    14. //如果字典只包含属性列表对象(NSData,NSDate,NSNumber,NSString,NSArray或NSDictionary)可以保存到文件中  
    15. NSString *filePath = [[[NSBundlemainBundle]resourcePath]stringByAppendingPathComponent:@"test_dict.plist"];  
    16. [test_dictionary writeToFile:filePath atomically:YES];  
    17. //用文件填充  
    18. NSDictionary *myDict =[NSDictionary dictionaryWithContentsOfFile:filePath];  
    19.   
    20. //可变字典  
    21. NSMutableDictionary *dictMutable = [[NSMutableDictionary alloc]initWithObjectsAndKeys:m_array,@"sort",n_array,@"number", nil];  
    22. NSString *str = @"10_10";  
    23. //修改对象  
    24. [dictMutable setObject:string4 forKey:@"sort"];  
    25. //删除对象  
    26. [dictMutable removeObjectForKey:@"number"];  
    27. //删除多个对象  
    28. NSArray *key_array =[NSArray arrayWithObjects:@"sort",@"number", nil];  
    29. [dictMutable removeObjectForKey:key_array];  
    30. //删除所有对象  
    31. [dictMutable removeAllObjects]; 

     

     

        //k-v 键值对

        //key:对象标识符,不重复

        //value:对象,可重复

        //字典与数组的区别:数组有序,字典无序;

        //分 可变字典 不可变字典

        

        //创建三个person对象

        Person *person1 = [Person PersonWithName:@"xi" age:18];

        Person *person2 = [Person PersonWithName:@"aa" age:9];

        Person *person3 = [Person PersonWithName:@"ao" age:3];

     

        

        //将对象添加到字典中

        //

        NSDictionary *pDict = [[NSDictionary alloc]initWithObjectsAndKeys:

                                        person1,@"xi",

                                        person2,@"aa",

                                        person3,@"ao",   nil];

        

        //便利构造器

        NSDictionary *pD = [NSDictionary dictionaryWithObjects:@[person1,person2,person3] forKeys:@[@"xi",@"aa",@"ao"]];

        NSLog(@"%@",pDict);

        

        //字面量创建 不可变

        NSDictionary *pDict1 = @{ @"xi":person1,

                                  @"aa":person2,

                                  @"ao":person3 };

        

        NSLog(@"%@",pDict);

     

     

        NSLog(@"%@",pDict);

        NSLog(@"-----------------使用枚举器,便利-------------------------");

       NSEnumerator *keyEnum = [pDict1 keyEnumerator];

        id obj;

        while (  obj = [keyEnum nextObject]) {

            NSLog(@"key=%@",obj);

        }

        

        NSEnumerator *keyEnum1 = [pDict1 objectEnumerator];

        id obj1;

        while (  obj1 = [keyEnum1 nextObject]) {

            NSLog(@"value=%@",obj1);

        }

     

        

        

        NSLog(@"-----------------使用枚举器,便利-------------------------");

        

     

     

     

        //从字典取出一个对象

        Person *p = [pDict objectForKey:@"xi"];

        Person *p1 = pDict[@"xi"];

        NSLog(@"%@",p);

        NSLog(@"%@",p1);

        

        

        //打印所有key

        NSLog(@"key=%@",[pDict allKeys]);

        

        //打印所有value

        NSLog(@"value=%@",[pDict allValues]);

        

        //循环的打印字典中的键值对

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

            //先通过循环 从allkeys 数组 取出每一个key

            NSString *key = [pDict allKeys][i];

            Person *p = pDict[key];

             NSLog(@"key=%@,value=%@",key, p);//调用description方法

        }

        

        //可变字典

        //1,创建

        NSMutableDictionary *mutaDic = [NSMutableDictionary dictionary];

        

        NSMutableDictionary *mutaDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:person1,@"ao",person2,@"xi", nil];

        

        //2,添加

        [mutaDic1 setObject:person3 forKey:@"aa"];

        

        

        

        //3,替换

        [mutaDic1 setObject:person3 forKey:@"ao"];

        

        //4,根据key去删除value

        [mutaDic1 removeObjectForKey:@"ao"];

        

        //5,删除所有

        [mutaDic1 removeAllObjects];

        //

        //

        

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

            //先通过循环 从allkeys 数组 取出每一个key

            NSString *key = [mutaDic1 allKeys][i];

            Person *p = mutaDic1[key];

            NSLog(@"key=%@,value=%@",key, p);//调用description方法

        }

        

        

        //1,创建一个数组,

        NSArray *classArray = @[@"zhang",@"zhu"];

        //2,创建一个数组,

        NSArray *classArray1 = @[@"zha",@"zh"];

        

        //1,创建一个大数组,

        NSArray *classArrayB = @[classArray,classArray1];

        

        NSDictionary *allClassDict = @{@"36": classArray,

                                       @"37": classArray1,

                                       };

        

        NSLog(@"%@",allClassDict[@"36"][0]);

        

        

        

        //NSset  元素唯一,无序,随机抽取,

        //1,

        NSSet *set = [NSSet setWithObjects:@"c",@"d",@"a", nil];

     

        NSLog(@"%@",set);

        NSLog(@"%@",[set anyObject]);

        

        

        if ([set containsObject:@"c"]) {

            NSLog(@"you c");

        }

        

        //1,创建 年龄数组

        NSArray *ageArray = @[@12, @33, @65];

        

        //使用数组创建集合

        NSCountedSet *ageSet = [NSCountedSet setWithArray:ageArray];

        

        //年龄是33的个数

        NSLog(@"%lu",[ageSet countForObject:@33 ]);

        

        

        //快速枚举

        

        NSArray *arr = @[@"wang",@"jin",@"wei",@11];

        

        for (id a in arr) {

            NSLog(@"%@",a);

        }

        

        

        NSDictionary *dict = @{@"11": @"v1",

                               @"22": @"v2"};

        //默认取key

        for (NSString * key in dict) {

            NSLog(@"%@",key);

        }

        

        //取value的值

        for (NSString * value in [dict allValues]) {

            NSLog(@"%@",value);

        }

        

        

        

        //创建一个保存年龄的数组

        NSMutableArray *mArr = [NSMutableArray arrayWithObjects:@"12", @"2", @"9", @"21", nil];

        

        

        for (int  i = 0; i < mArr.count - 1; i++) {

            for (int j = 0; j < mArr.count - 1 - i; j++) {

                if ([mArr[j] intValue] >  [mArr[j+1] intValue]) {

                   

                   [mArr exchangeObjectAtIndex:j withObjectAtIndex:j+1];

                 

                }

            }

        }

        NSLog(@"%@",mArr);

        

        //默认升序

        [mArr sortedArrayUsingSelector:@selector(compare:)];

        NSLog(@"%@",mArr);

        

        

        

    //    //创建三个person对象

    //    Person *person1 = [Person PersonWithName:@"xi" age:18];

    //    Person *person2 = [Person PersonWithName:@"aa" age:19];

    //    Person *person3 = [Person PersonWithName:@"ao" age:38];

    //    

        NSMutableArray *personArray = [NSMutableArray arrayWithObjects:person1, person2, person3, nil];

        NSLog(@"%@",personArray);

        

        //按年龄从小到大排序 

        for (int i = 0; i < personArray.count - 1; i++) {

            for (int j = 0; j < personArray.count - i - 1;j++) {

                if ([personArray[j] age] > [personArray[j + 1] age] ) {

                    [personArray exchangeObjectAtIndex:j withObjectAtIndex:j + 1];

                }

            }

        

        }

        

        NSLog(@"%@",personArray);

     

     

     

    NSDictionary的常见用法总结

            NSArray *array1 = [NSArray arrayWithObjects:@"iphone",@"ipod",nil];

            NSArray *array2 = [NSArray arrayWithObjects:@"mac",@"imac",@"mac pro",nil];

            //类方法初始化自动释放

            NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:array1,@"mobile",array2,@"computers",nil];//注意用nil结束

            NSLog(@"myDictionary = %@",myDictionary);

            

            int dictSize = [myDictionary count];

            //访问字典中的值

            NSArray *mobile = [myDictionary objectForKey:@"mobile"];

            //从一个对象获取键

            NSArray *keys = [myDictionary allKeysForObject:array1];

            //获取字典中所有值得一个数组

            NSArray *values = [myDictionary allValues];

            //快速枚举

            for(id key in myDictionary)

            {

                NSLog(@"key: %@,value: %@",key,[myDictionary objectForKey:key]);

            }

            //如果字典只包含属性列表对象(NSData,NSDate,NSNumber,NSString,NSArray或NSDictionary)可以保存到文件中

            NSString *filePath = [[[NSBundlemainBundle]resourcePath]stringByAppendingPathComponent:@"dict.txt"];

            BOOL success = [myDictionary writeToFile:filePath atomically:YES];

            //用文件填充

            NSDictionary *myDict2 =[NSDictionary dictionaryWithContentsOfFile:filePath];

            

            //可变字典

            NSMutableDictionary *dictMutable = [[NSMutableDictionary alloc]initWithObjectsAndKeys:array1,@"mobile",array2,@"computer", nil];

            NSString *string4 = @"stringTV";

            //修改对象

            [dictMutable setObject:string4 forKey:@"media"];

            //删除对象

            [dictMutable removeObjectForKey:@"mobile"];

            //删除多个对象

            NSArray *keyArray =[NSArray arrayWithObjects:@"mobile",@"computer", nil];

            [dictMutable removeObjectForKey:keyArray];

            //删除所有对象

            [dictMutable removeAllObjects];

     

    注: iOS 6 新的快捷初始化写法:

    NSDictionary:

     

    [csharp] view plaincopy
    1. NSDictionary *dic = @{@"键":@"值",@"键1":@"值1"};  

     

    NSMutableDictionary:

     

    [csharp] view plaincopy
    1. NSMutableDictionary *MDic = [@{@"键":@"值",@"键1":@"值1"} mutableCopy];  

     

     



     

     

    1:基础初始化

    [csharp] view plaincopy
    1. NSMutableDictionary *muDicAsyncImage = [[NSMutableDictionary alloc] init];  

    2:为字典添加对象(键与值都是 id 接受任何类型)

    [csharp] view plaincopy
    1. [muDicAsyncImage setObject:@"值" forKey:@"键"];  

    3:通过键取得值对象

     

    [csharp] view plaincopy
    1. NSString *str= [muDicAsyncImage objectForKey:@"键"];  

     

    4:删除某个对象

     

    [csharp] view plaincopy
      1. [ muDicAsyncImage removeObjectForKey: @"键"];  

     

     

     

  • 相关阅读:
    图书管理系统(spring springmvc)
    ssm框架整合(java个人博客系统)
    HTTP笔记
    (数据结构课程设计)稀疏矩阵运算器
    数据库学习笔记
    HTML5 3D旋转图片相册
    Mybatis分页插件-PageHelper的使用
    如何更改github工程的语言属性
    HttpServletResponse和HttpServletRequest详解
    node js 安装.node-gyp/8.9.4 权限 无法访问
  • 原文地址:https://www.cnblogs.com/iOS-mt/p/4108968.html
Copyright © 2011-2022 走看看