iOS 字典的 addEntriesFromDictionary使用注意点

时间:2021-07-08 03:11:24
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"BMW",@"CarLogo",@"Red",@"CarColor",@"Xiaoming1",@"name", nil];
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Xiaoming",@"name",@"28", @"age",nil];
    [dic1 addEntriesFromDictionary:dic2];
    
    NSLog(@"%@",dic1);
红色标记部分是相同key,这种情况输出结果如下:
{
    CarColor = Red;
    CarLogo = BMW;
    age = 28;
    name = Xiaoming;
}



下面看第二种情况:

    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"BMW",@"CarLogo",@"Red",@"CarColor",@"Xiaoming1",@"name",nil];
    NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Xiaoming",@"name",@"28", @"age", nil];
    [dic2 addEntriesFromDictionary:dic1];
    
    NSLog(@"%@",dic2);
输出如下:
{
    CarColor = Red;
    CarLogo = BMW;
    age = 28;
    name = Xiaoming1;
}

规律了然:
在相同key的情况下,相对应的value会被赋予新值,使用时注意顺序