iOS-NSDictionary和NSMutableDictionary用法大全详细说明

释放双眼,带上耳机,听听看~!
+ (void)NSDictionaryCreateMethod
{
    NSDictionary *dict = @{@"one":@"1",@"two":@"2",@"three":@"3",@"four":@"4"};
    NSLog(@"dict = %@",dict);
    
    //减方法
    NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three",@"4",@"four",nil];
    NSLog(@"dict1 = %@",dict1);
    
    //对应的加方法
    NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three",@"4",@"four",nil];
    NSLog(@"dict2 = %@",dict2);
    
    NSDictionary *dict3 = [[NSDictionary alloc] initWithDictionary:dict];
    NSLog(@"dict3 = %@",dict3);
    
    NSDictionary *dict4 = [NSDictionary dictionaryWithDictionary:dict];
    NSLog(@"dict4 = %@",dict4);
    
    NSArray *keys = @[@"one",@"two",@"three",@"four"];
    NSArray *objs = @[@"1",@"2",@"3",dict1];
    NSDictionary *dict5 = [[NSDictionary alloc] initWithObjects:objs forKeys:keys];
    NSLog(@"dict5 = %@",dict5);
    
    NSDictionary *dict6 = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
    NSLog(@"dict6 = %@",dict6);
 
}
 
+ (void)NSDictionarySimpleMethod
{
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three",@"4",@"four",nil];
    //字典中的键值数
    NSLog(@"count = %lu",[dict count]);
    
    //通过key找到对应的obj 不能通过obj  找到key
    NSLog(@"%@",[dict objectForKey:@"three"]);
    
    //遍历字典 枚举  或 快速枚举
    NSEnumerator *e1 = [dict keyEnumerator];
    id obj = nil;
    while (obj = [e1 nextObject])
    {
        NSLog(@"key:%@,value:%@",obj,[dict objectForKey:obj]);
    }
    
    NSEnumerator *e2 = [dict objectEnumerator];
    
    while (obj = [e2 nextObject])
    {
        NSLog(@"value:%@",obj);
    }
    
    //快速枚举 实际上是字典key的枚举
    for(id objin dict)
    {
        NSLog(@"obj = %@",obj);//和 NSLog(@"dict = %@",dict); 打印的顺序是不一样的
    }
    
    NSArray *keyArray = [dict allKeys];
    NSArray *objArray = [dict allValues];
    for(int i =0;i<[dict count];i++)
    {
        NSLog(@"key : %@,value : %@",[keyArray objectAtIndex:i],[objArray objectAtIndex:i]);
    }
    
}
 
+ (void)NSMutableDictionaryCreateMethod
{
    NSDictionary *dict = @{@"one":@"1",@"two":@"2",@"three":@"3",@"four":@"4"};
    NSMutableDictionary *dict1 = [[NSMutableDictionary alloc]initWithDictionary:dict];
    NSLog(@"dict1 = %@",dict1);
    
    //对应的加方法
    NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:dict];
    NSLog(@"dict2 = %@",dict2);
    
    //创建一个空的可变字典
    NSMutableDictionary *dict3 = [[NSMutableDictionary alloc] init];
    NSLog(@"dict3&count = %lu",[dict3 count]);
    
    //对应的加方法
    NSMutableDictionary *dict4 = [NSMutableDictionary dictionary];
    NSLog(@"dict4&count = %lu",[dict4 count]);
    
    //可变字典自己的初始化方式
    NSMutableDictionary *dict5 = [[NSMutableDictionary alloc] initWithCapacity:10];
    NSLog(@"dict5&count = %lu",[dict5 count]);
    
    NSMutableDictionary *dict6 = [NSMutableDictionary dictionaryWithCapacity:10];
    NSLog(@"dict6&count = %lu",[dict6 count]);
    
 
    
}
 
+ (void)NSMutableDictionarySimpleMethod
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three",@"4",@"four",nil];
    //增加字典元素  或者修改  若果没有增加 若果有修改 覆盖
    [dict setObject:@"5" forKey:@"five"];
    NSLog(@"%@",dict);
    [dict setObject:@"6666" forKey:@"five"];
    NSLog(@"%@",dict);
    
    //因为字典是无序的 没有 交换插入  追加
    //删除
    [dict removeObjectForKey:@"five"];
    NSLog(@"%@",dict);
    
    [dict removeAllObjects];
    NSLog(@"%@",dict);//变成空字典了
    
    //重设字典
    NSDictionary *dict1 = @{@"one":@"111",@"two":@"222",@"three":@"333",@"four":@"444"};
    [dict setDictionary:dict1];
    NSLog(@"%@",dict);//变成空字典了
    
    
}

 

人已赞赏
iOS文章

iOS-防止按钮重复点击

2019-10-31 5:07:06

iOS文章

iOS 定制应用程序字体

2019-10-31 8:46:22

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索