如何从 NSDictionary 中删除对象

发布于 2024-09-26 07:08:05 字数 770 浏览 4 评论 0原文

您好,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

各自安好 2024-10-03 07:08:05

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

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

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

[countriesToLiveInDict removeObjectForKey:@"Countries"];
[countriesToLiveInDict setObject:sortedArray forKey:@"Countries"];
别闹i 2024-10-03 07:08:05

首先将你的 NSDictionary 转为 NSMutableDictionary &然后编写以下代码行

[countriesToLiveInDict removeObjectForKey:@"Countries"];

这肯定会解决您的问题。

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

[countriesToLiveInDict removeObjectForKey:@"Countries"];

This will definitely resolve your issue.

别在捏我脸啦 2024-10-03 07:08:05

NSDictionary 无法删除任何内容,请使用 NSMutableDictionary,如下所示:

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

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

NSMutableDictionary *countriesToLiveInDict = [NSMutableDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];
苍景流年 2024-10-03 07:08:05

对于斯威夫特 3
正如@MathieuF 回答的那样
首先,您需要使用 NSMutableDictionary 并输入以下代码:

countriesToLiveInDict.removeObject(forKey: "Countries")

我发布我的答案,因为我正在搜索相同的问题并受到 @MathieuF 的启发

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

countriesToLiveInDict.removeObject(forKey: "Countries")

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文