Foundation框架 - NSDictionary类NSMutableDictionary类

Posted liguangsunls

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Foundation框架 - NSDictionary类NSMutableDictionary类相关的知识,希望对你有一定的参考价值。

NSArray、NSSet、NSDictionary

/*
 集合
 1.NSArray\NSMutableArray
 * 有序
 * 高速创建(不可变):@[obj1, obj2, obj3]
 * 高速訪问元素:数组名[i]

 2.NSSet\NSMutableSet
 * 无序

 3.NSDictionary\NSMutableDictionary
 * 无序
 * 高速创建(不可变):@{key1 : value1,  key2 : value2}
 * 高速訪问元素:字典名[key]
 */

字典创建

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"**************************  ******************************");

        //方式一:
         NSDictionary *dict = [NSDictionary dictionaryWithObject:@"jack" forKey:@"name"];

        //方式二:
         NSArray *keys = @[@"name", @"address"];
         NSArray *objects = @[@"jack", @"BeiJing"];
         NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
         NSLog(@"集合二结果为:%@",dict2);

        //方式三:值健对
         NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:
         @"jack", @"name",
         @"BeiJing", @"address",
         @"88888888", @"qq", nil];
         NSLog(@"集合三结果为:%@",dict3);

字典高速初始化

        NSLog(@"************************ 字典高速初始化 **************************");

        //健值对
        NSDictionary *dict5 = @{@"name" : @"jack", @"address" : @"Beijing"};
        id obj = dict5[@"name"];
        NSLog(@"%@", obj);

        // 返回的是键值对的个数
        NSLog(@"%ld", dict5.count);

字典创建实例

        NSLog(@"************************ 字典创建实例 ****************************");

        //界面语言,程序窗口标题,确定按钮文字,提示输入
        NSArray* values=[NSArray arrayWithObjects:@"欢迎登录",@"登录",@"请输入username:", nil];
        NSArray* keys1=[NSArray arrayWithObjects:@"window_title",
                        @"confirm_button",@"input_hint", nil];
        NSDictionary* dict4=[NSDictionary dictionaryWithObjects:values forKeys:keys1];
        NSLog(@"count:%lu",dict.count);

        //NSEnumerator能够使用for-each循环高速进行迭代
        NSEnumerator*keyEnums=[dict4 keyEnumerator];
        int i=0;
        for (NSString* key in keyEnums) {
            NSLog(@"key%d:%@",i++,key);
        }
        NSLog(@"window_title=%@",[dict4 objectForKey:@"window_title"]);
        NSLog(@"confirm_button=%@",[dict4 objectForKey:@"confirm_button"]);
        NSLog(@"input_hint=%@",[dict4 objectForKey:@"input_hint"]);

可变字典

        NSLog(@"*************************** 可变字典 *****************************");


        NSMutableDictionary *dict6 = [NSMutableDictionary dictionary];
        // 加入值健对
        [dict6 setObject:@"jack" forKey:@"name"];
        [dict6 setObject:@"BeiJing" forKey:@"address"];
        [dict6 setObject:@"1206293008" forKey:@"QQ"];
        [dict6 setObject:@"male" forKey:@"sex"];
        [dict6 setObject:@"programming" forKey:@"hobby"];
         NSLog(@"%@", dict6);
        // 移除值健对
         [dict6 removeObjectForKey:@"address"];

        //输出姓名
         NSString *str = dict6[@"name"];
         NSLog(@"%@", str);

字典遍历

          NSLog(@"************************** 字典遍历 ******************************");


        // 方式一:字典是无序的
         NSDictionary *dict7 = @{
                               @"address" : @"北京",
                               @"name" : @"jack",
                               @"qq" : @"88888888"};

            NSArray *keys2 = [dict7 allKeys];
            for (int i = 0; i<dict7.count; i++)
            {
                NSString *key = keys2[i];
                NSString *object = dict7[key];
                NSLog(@"%@ ~~~~~~~ %@", key, object);
            }

        //方式二:block遍历
        [dict7 enumerateKeysAndObjectsUsingBlock:
         ^(id key, id obj, BOOL *stop) {
             NSLog(@"%@ ---- %@", key, obj);

         }];

字典需求排序

        NSLog(@"************************* 字典需求排序 ****************************");


        NSMutableDictionary* givenName=[NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithInt:4],@"王", nil];
        [givenName setObject:[NSNumber numberWithInt:8] forKey:@"周"];
        [givenName setObject:[NSNumber numberWithInt:6] forKey:@"李"];
        [givenName setObject:[NSNumber numberWithInt:9] forKey:@"赵"];

        //keySortedByValueUsingSelector
        //由于value是NSNumber类型。全部能够使用compare:进行比較
        NSArray* keysOfName=[givenName keysSortedByValueUsingSelector:@selector(compare:)];
        for (NSString* key in keysOfName) {
            NSLog(@"key:%@",key);
        }
        //高速迭代(枚举)
        for (NSString * key in givenName) {
            NSNumber* value=[givenName valueForKey:key];
            NSLog(@"[%@]=%@",key,value);
        }

    }
    return 0;

以上是关于Foundation框架 - NSDictionary类NSMutableDictionary类的主要内容,如果未能解决你的问题,请参考以下文章

iOS-Foundation框架—结构体(转载)

cocoa和foundation框架的区别

34-oc Foundation简介

OC 知识:Foundation 框架及相关类详尽总结

强大又简单的响应式框架——Foundation 网格系统

Foundation框架—字符串