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

字典类

时间:2014-12-30 23:33:25      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

//1.创建字段,不可变的
        NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"xiaozhe",@"name", nil];
        NSLog(@"dic %@",dic);
        
        //2.快捷创建方式
        NSDictionary * dic2 = @{ @"one":@"1",@"two":@"2"};
        NSLog(@"dic2 %@",dic2);
        
        //3.字典中可以存任意数据类型
        // 字典的顺序不是自然顺序
        NSArray * array = @[@"one",@"two"];
        
        NSDictionary * dic3 = @{
                                @"one":@"1",
                                @"num":[NSNumber numberWithInt:10],
                                @"aaa":dic2,
                                @"bbb":dic,
                                @"ar1":array
                                
                                };
        
        NSLog(@"dic3 %@",dic3);
        
        //4.获得字典的长度
        NSLog(@"count %ld",dic3.count);
        
        //5.从字典中取值
        
        NSString * str  = [dic3 objectForKey:@"one"];
        NSLog(@"str %@",str);
        
        NSDictionary * dicTmp = [dic3 objectForKey:@"aaa"];
        NSLog(@"dicTmp %@",dicTmp);
        NSArray * arrayTmp = [dic3 objectForKey:@"ar1"];
        NSLog(@"arrayTmp %@",arrayTmp);
        
        
        
        
        //6.遍历
        
        //取出所有的key值
        NSArray * allkeys = [dic3 allKeys];
        NSLog(@"allkeys %@",allkeys);
        
        for (int i = 0; i < allkeys.count; i++)
        {
            NSString * key = [allkeys objectAtIndex:i];
            
            //如果你的字典中存储的多种不同的类型,那么最好用id类型去接受它
            id obj  = [dic3 objectForKey:key];
            NSLog(@"obj %@",obj);
        }
    
        //2.枚举器
        
        NSEnumerator * enumerator =  [dic3 objectEnumerator];
        
        id value;
        while (value = [enumerator nextObject]) {
            NSLog(@"value %@",value);
        }
        

 

字典类

标签:

原文地址:http://www.cnblogs.com/cwhking/p/4194655.html

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