NSDictionary基础-创建、初始化、赋值、获取、复制

代码:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //创建字典
        NSNumber *num=[NSNumber numberWithInt:100];
        NSDictionary *dic1=[NSDictionary dictionaryWithObject:num forKey:@"money"];
        NSLog(@"%@",dic1);
        //同时传入多个值,依次是value,key,value,key...
        NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:@"Jay",@"name",@"22",@"age",@"Female",@"Sex",nil];
        NSLog(@"%@",dic2);
        //直接赋值
        NSDictionary *dic3=@{@"name":@"White",
                             @"age":@"22",
                             @"sex":@"Male"};
        NSLog(@"%@",dic3);
        //复制字典
        NSDictionary *dic4=[NSDictionary dictionaryWithDictionary:dic3];
        //获取key-value的对数
        NSUInteger count=[dic4 count];
        NSLog(@"count=%ld",count);
        //根据key获取value
        NSString *name=[dic3 objectForKey:@"name"];
        NSLog(@"%@",name);
        //获取字典中所有value
        NSArray *arr1=[dic3 allValues];
        NSLog(@"%@",arr1);
        //获取字典中所有key
        NSArray *arr2=[dic3 allKeys];
        NSLog(@"%@",arr2);
    }
    return 0;
}

 

posted @ 2016-01-08 10:26  零点五  阅读(1990)  评论(0编辑  收藏  举报