• NSDictionary的初始化及用法


    NSDictionary *dictionary=[NSDictionary dictionaryWithObject:@"aa" forKey:@"1"   ];

            NSLog(@"%@",dictionary);

            

            NSDictionary *dictionary1=[NSDictionary dictionaryWithObjectsAndKeys:@"aaa",@"1", nil];

            NSLog(@"%@",dictionary1);

            

            NSDictionary *dictionary3 = @{@"1":@"a",@"2":@"aa",@"3":@"aaa"};

            NSLog(@"%@",dictionary3);

            

            NSDictionary *dictionary2=[NSDictionary dictionaryWithDictionary:dictionary];

            

            //字典的方法:

            

            //字典键值对的个数

            NSLog(@"%zi",dictionary3.count);

            

            //获取某一关键字的值

            NSString *str = [dictionary3 objectForKey:@"2"];

            NSLog(@"%@",str);

            

            //取出所有关键对应的值

            //先取出字典中所有的键,放入一个数组中,然后对这个数组便利

            NSArray *arr = [dictionary3 allKeys];//取出所有的键值

            for (NSString *obj in arr){

                NSLog(@"%@",dictionary3[obj]);//根据键取值

            }

            //使用枚举器取出值

            NSString *obj1;

            NSEnumerator *e = [dictionary3 objectEnumerator];//获取所有value的值

            while (obj1=[e nextObject]) {

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

            }

           

            //stop代表遍历停止

            [dictionary3 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

                if (stop) {

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

                }

            }];

  • 相关阅读:
    [Objective-c 基础
    [Objective-c 基础
    [Objective-c 基础
    [Objective-c 基础
    [Objective-c 基础
    [Objective-c 基础
    [Objective-c 基础
    [Objective-c 基础
    [Objective-c 基础
    39. Combination Sum(dfs)
  • 原文地址:https://www.cnblogs.com/banchuangshuying/p/4937079.html
Copyright © 2020-2023  润新知