如何从NSDictionary中删除对象

时间:2022-10-09 13:49:35

Hi i am having a NSdictionary in which i am adding a array with key "countries ". Now i take the value of this dictionary into array and sort the array in alpahbatical order .Now i want to add this array into my Dictionary (that is i want to update my dictionary with new sorted array and remove the old array from it )........ how to do this

嗨,我有一个NSdictionary,我在其中添加了一个带有关键“国家”的数组。现在我把这个字典的值放入数组中,按照alpah顺序对数组进行排序,现在我想把这个数组添加到我的字典中(也就是我想用新的排序数组更新我的字典,从里面删除旧的数组)…如何做到这一点

My code is as follows

我的代码如下

NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:@"Iceland", @"Greenland", @"Switzerland", @"Norway", @"New Zealand", @"Greece", @"Italy", @"Ireland", nil];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];


NSArray *tmpary = [countriesToLiveInDict valueForKey:@"Countries"];
NSLog(@"ary value is  %@",ary);
NSArray *sortedArray = [tmpary sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSLog(@"sortedArray is %@",sortedArray);

Here i want to remove the countriesToLiveInArray and replace it with sortedArray with same key value i.e. Countries Thanks in advance..

在这里,我想去除国家的政治色彩,用具有相同关键价值的山药替代,即国家先行感谢。

4 个解决方案

#1


63  

First you need to use a NSMutableDictionary and put this code :

首先,你需要使用一个NSMutableDictionary,并输入以下代码:

[countriesToLiveInDict removeObjectForKey:@"Countries"];
[countriesToLiveInDict setObject:sortedArray forKey:@"Countries"];

#2


5  

First of all make your NSDictionary to NSMutableDictionary & then write the following line of code

首先,把你的NSDictionary变成NSMutableDictionary,然后写出下面一行代码。

[countriesToLiveInDict removeObjectForKey:@"Countries"];

This will definitely resolve your issue.

这肯定能解决你的问题。

#3


1  

NSDictionary cannot remove anything, please use NSMutableDictionary, like this:

NSDictionary不能删除任何内容,请使用NSMutableDictionary,如下:

NSMutableDictionary *countriesToLiveInDict = [NSMutableDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];

#4


0  

for Swift 3 as @MathieuF answered it First you need to use a NSMutableDictionary and put this code :

对于Swift 3 (@MathieuF)来说,首先需要使用一个NSMutableDictionary,并输入以下代码:

countriesToLiveInDict.removeObject(forKey: "Countries")

i post my answer as i was search for same question and get inspired by @MathieuF

当我在搜索同样的问题时,我发布了我的答案,并得到了@MathieuF的启发。

#1


63  

First you need to use a NSMutableDictionary and put this code :

首先,你需要使用一个NSMutableDictionary,并输入以下代码:

[countriesToLiveInDict removeObjectForKey:@"Countries"];
[countriesToLiveInDict setObject:sortedArray forKey:@"Countries"];

#2


5  

First of all make your NSDictionary to NSMutableDictionary & then write the following line of code

首先,把你的NSDictionary变成NSMutableDictionary,然后写出下面一行代码。

[countriesToLiveInDict removeObjectForKey:@"Countries"];

This will definitely resolve your issue.

这肯定能解决你的问题。

#3


1  

NSDictionary cannot remove anything, please use NSMutableDictionary, like this:

NSDictionary不能删除任何内容,请使用NSMutableDictionary,如下:

NSMutableDictionary *countriesToLiveInDict = [NSMutableDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];

#4


0  

for Swift 3 as @MathieuF answered it First you need to use a NSMutableDictionary and put this code :

对于Swift 3 (@MathieuF)来说,首先需要使用一个NSMutableDictionary,并输入以下代码:

countriesToLiveInDict.removeObject(forKey: "Countries")

i post my answer as i was search for same question and get inspired by @MathieuF

当我在搜索同样的问题时,我发布了我的答案,并得到了@MathieuF的启发。