码迷,mamicode.com
首页 > 其他好文 > 详细

Foundation框架 - NSDictionary类、NSMutableDictionary类

时间:2015-03-19 10:13:19      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:nsmutabledictionary   nsdictionary   集合   字典   

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:@"欢迎登录",@"登录",@"请输入用户名:", 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类

标签:nsmutabledictionary   nsdictionary   集合   字典   

原文地址:http://blog.csdn.net/wangzi11322/article/details/44452853

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!