我有一个NSDicitonary,并将其转换为NSMutableDictionary。我正在尝试对NSMutableDictionary进行更改,现在即使在NSDictionary中也如何实现这些更改?

NSDictionary *dct=[[NSDictionary alloc] initWithObjectsAndKeys:@"varun",@"name",@"9703507750",@"mobileNo",nil];

NSLog(@"dct : %@",dct);
dctMute=[[NSMutableDictionary alloc] init];

dctMute=[dct mutableCopy];
NSLog(@"dctMute :  %@",dctMute);

[dctMute setObject:@"hyderabad" forKey:@"city"];
[dctMute setObject:@"varun kumar" forKey:@"name"];
NSLog(@"dctMute after adding city:  %@",dctMute);

最佳答案

您不需要可变的副本。创建一个可变的字典,然后使其不变,如下所示:

NSMutableDictionary *dctMute = [NSMutableDictionary dictionary];
dctMute[@"foo"] = @"bar";
NSDictionary *immutableDictionary = [NSDictionary dictionaryWithDictionary:dctMute];

使用新语法,您也可以通过以下方式初始化:
NSDictionary *immutableDictionary = @{@"foo": @"bar"};

关于iphone - 将NSDictionary转换为NSMutableDicitonary时,如何影响更改?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18593976/

10-10 21:14