//

//  main.m

//  可变字典

//

//  Created by 博博 on 16/1/8.

//  Copyright (c) 2016年 com.bb. All rights reserved.

//

#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])

#import <Foundation/Foundation.h>

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        

        

        

        

        NSMutableDictionary *mudic=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"jay",@"name",@"22",@"age",@"f",@"gender", nil];

        //NSLog(@"%@",mudic);

        NSDictionary *dic=[NSDictionary dictionaryWithObject:@"166" forKey:@"height"];//法1

        [mudic addEntriesFromDictionary:dic];                                         //法1

        [mudic setValue:@"66" forKey:@"weight"];                                      //法2

        //NSLog(@"%@",mudic);

        

        NSMutableDictionary *nulldic=[NSMutableDictionary dictionary];

        //将字典nulldic设置与字典mudic对象相同

        [nulldic setDictionary:mudic];

        //NSLog(@"%@",nulldic);

        //将字典中对应key的值删除

        [nulldic removeObjectForKey:@"weight"];

        //NSLog(@"%@",nulldic);

        NSArray *keys=[NSArray arrayWithObjects:@"heighr",@"gender", nil];

        [nulldic removeObjectsForKeys:keys];

        //NSLog(@"%@",nulldic);

        [nulldic removeAllObjects];

        //NSLog(@"%@",nulldic);

        //遍历

        [nulldic setDictionary:mudic];

        //1.先找到所有key;2.计算key的个数,用于循环遍历条件;3通过key 的数组找到对应key值

        NSArray *keyss=[nulldic allKeys];

        NSInteger count=[nulldic count];

        for(int i=0;i<nulldic.count;i++)

        {

            id key=[keyss objectAtIndex:i];

            //NSLog(@"%@",[nulldic objectForKey:key]);

        }

        //2.快速枚举

        for (id key in nulldic)

        {

            id obj=[nulldic objectForKey:key];

            NSLog(@"%@",obj);

        }

        //3.枚举对象(c语言中的枚举enum) 通过枚举对象进行枚举

        //将字典里的key转成枚举对象,用于遍历/枚举

        NSLog(@"/////////");

        NSEnumerator *keyenum=[nulldic keyEnumerator];

        id key=[keyenum nextObject];

        while (key=[keyenum nextObject]) {

            

            id obj=[nulldic objectForKey:key];

            NSLog(@"%@",obj);

            key=[keyenum nextObject];

            

        }

    }

    return 0;

}

 

posted on 2016-01-11 13:27  bobohahaha  阅读(114)  评论(0编辑  收藏  举报