一.字典创建

1.快速创建字典

NSDictionary *dic1 = @{@"1":@"one",@"2":@"two"};
NSLog(@"dic1 is %@",dic1);

2.通过构造方法创建字典

NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"one",@"1",@"two",@"2",nil];
NSLog(@"dic is %@",dic);

二.字典操作

首先创建一个字典

NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3",nil];

1.通过键获取值

NSString *key = @"2";
NSString *value = [dic objectForKey:key];
NSLog(@"键为%@的值为%@",key,value);

  输出结果:

2016-06-27 19:14:56.682 OcTest[1160:883000] 键为2的值为two
Program ended with exit code: 0

2.字典遍历

a.迭代器遍历

//      NSEnumerator *enu = [dic keyEnumerator];  //通过键迭代器得到的是键
        NSEnumerator *enu = [dic objectEnumerator];  //通过值迭代器得到的是值
        id obj;
        while(obj = [enu nextObject]){
            NSLog(@"%@",obj);  //键迭代器得到的是键
            NSLog(@"%@",[dic objectForKey:obj]);  //值迭代器获取值
        }

b.快速遍历法

        for (obj in dic){
            NSLog(@"%@",obj);  //键迭代器得到的是键
            NSLog(@"%@",[dic objectForKey:obj]);  //值迭代器获取值
        }

三.可变字典NSMutableDictionary

1.可变字典的创建同不可变字典的创建:可以通过构造方法或者类方法创建

2.可变字典的特性

首先我们创建一个空的可变字典

a.添加键值对

        [mDic setObject:@"one" forKey:@"1"];
        [mDic setObject:@"two" forKey:@"2"];
        [mDic setObject:@"three" forKey:@"3"];
        [mDic setObject:@"tem" forKey:@"1"];  //会覆盖掉键1的值

四.网络中url应用

网络中我们会把url给拼接起来,就可以用到可变字典

 

posted on 2016-06-25 17:23  chengkaihua  阅读(171)  评论(0编辑  收藏  举报