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

可变字典

时间:2015-01-01 08:56:57      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

//1.创建一个可变长度字典
        NSMutableDictionary * muDic = [[NSMutableDictionary alloc] initWithCapacity:0];
        //2.向字典中村如数据
        [muDic setObject:@"1" forKey:@"one"];
        [muDic setObject:@"2" forKey:@"two"];
        [muDic setObject:@"3" forKey:@"three"];
        
        NSLog(@"muDic %@",muDic);
        
        //3.删除
        [muDic removeObjectForKey:@"one"];
        
        NSLog(@"muDic %@",muDic);
        
        //4.全部删除
        [muDic removeAllObjects];
        
        NSLog(@"muDic %@",muDic);
        
        
        Student * stu1 = [[Student alloc] initWithName:@"xiaoher" andAge:20];
        Student * stu2 = [[Student alloc] initWithName:@"alex" andAge:50];
        Student * stu3 = [[Student alloc] initWithName:@"xiaoli" andAge:10];
        
        [muDic setObject:stu1 forKey:@"s1"];
        [muDic setObject:stu2 forKey:@"s2"];
        [muDic setObject:stu3 forKey:@"s3"];
        //在向字典中存储数据的时候,一定要保证key值是唯一的
//        [muDic setObject:stu3 forKey:@"s3"];
//        [muDic setObject:stu3 forKey:@"s3"];
//        [muDic setObject:stu3 forKey:@"s3"];
    
        
        NSLog(@"muDic %@",muDic);
        
        
        //1.使用for循环遍历字典
        NSArray * allkeys = [muDic allKeys];
        
        for (int i = 0; i < allkeys.count; i++)
        {
            NSString * key = [allkeys objectAtIndex:i];
            Student * stu = [muDic objectForKey:key];
            NSLog(@"stu %@",stu);
        };
        
        //2.使用枚举器
        
        NSEnumerator * enumerator = [muDic objectEnumerator];
        Student * tmp;
        while (tmp = [enumerator nextObject]) {
            NSLog(@"tmp %@",tmp);
        }

 

可变字典

标签:

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

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