如何从 NSDictionary 中删除对象
您好,我有一个 NSdictionary,其中我添加了一个带有键“国家”的数组。现在我将此字典的值放入数组中并按字母顺序对数组进行排序。现在我想将此数组添加到我的字典中(也就是说我想用新的排序数组更新我的字典并从中删除旧数组)。 ......如何做到这一点
我的代码如下
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);
这里我想删除countriesToLiveInArray并将其替换为具有相同键值的sortedArray,即国家 提前致谢..
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
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您需要使用
NSMutableDictionary
并输入以下代码:First you need to use a
NSMutableDictionary
and put this code :首先将你的 NSDictionary 转为 NSMutableDictionary &然后编写以下代码行
这肯定会解决您的问题。
First of all make your NSDictionary to NSMutableDictionary & then write the following line of code
This will definitely resolve your issue.
NSDictionary
无法删除任何内容,请使用NSMutableDictionary
,如下所示:NSDictionary
cannot remove anything, please useNSMutableDictionary
, like this:对于斯威夫特 3
正如@MathieuF 回答的那样
首先,您需要使用 NSMutableDictionary 并输入以下代码:
我发布我的答案,因为我正在搜索相同的问题并受到 @MathieuF 的启发
for Swift 3
as @MathieuF answered it
First you need to use a NSMutableDictionary and put this code :
i post my answer as i was search for same question and get inspired by @MathieuF