ios 如何从 NSDictionary 中删除对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3879546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to remove object from NSDictionary
提问by V.V
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,我在其中添加了一个带有关键“国家/地区”的数组。现在我将该字典的值放入数组并按字母顺序对数组进行排序。现在我想将此数组添加到我的字典中(即我想用新的排序数组更新我的字典并从中删除旧数组)。 ....... 这该怎么做
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..
在这里,我想删除 countriesToLiveInArray 并用具有相同键值的 sortedArray 替换它,即 Country 提前致谢..
回答by MathieuF
First you need to use a NSMutableDictionary
and put this code :
首先,您需要使用 aNSMutableDictionary
并放置此代码:
[countriesToLiveInDict removeObjectForKey:@"Countries"];
[countriesToLiveInDict setObject:sortedArray forKey:@"Countries"];
回答by Atulkumar V. Jain
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.
这绝对可以解决您的问题。
回答by Donald
NSDictionary
cannot remove anything, please use NSMutableDictionary
, like this:
NSDictionary
无法删除任何内容,请使用NSMutableDictionary
,如下所示:
NSMutableDictionary *countriesToLiveInDict = [NSMutableDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];
回答by Amr Angry
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 的启发