如何从NSDictionary中删除对象 [英] how to remove object from NSDictionary

查看:90
本文介绍了如何从NSDictionary中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用NSdictionary,我在其中添加了一个包含关键国家/地区的数组。现在我把这个字典的值放到数组中并按照alpahbatical顺序对数组进行排序。现在我想将这个数组添加到我的字典中(我希望用新的排序数组更新我的字典并从中删除旧数组)。 .......怎么做

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

我的代码如下

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,即国家
提前致谢..

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

推荐答案

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

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

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

这篇关于如何从NSDictionary中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆