如何将 NSDictionary 转换为 NSMutableDictionary?

发布于 2024-12-03 08:15:58 字数 436 浏览 2 评论 0原文

我有一个现有的 NSDictionary,其中包含:

{
    "charts_count" = 2;
    "created_at" = "2010-04-12T16:37:32Z";
    exchange = NASDAQ;
    "followers_count" = 259;
    id = 8404;
    industry = "<null>";
    "messages_count" = 1436;
    ric = "GRPN.O";
    sector = "<null>";
    symbol = GRPN;
    title = Groupon;
    "updated_at" = "2011-09-05T04:17:56Z";
}

如何获取这些内容并将其放入新的 NSMutableDictionary 中?

I have an existing NSDictionary that has:

{
    "charts_count" = 2;
    "created_at" = "2010-04-12T16:37:32Z";
    exchange = NASDAQ;
    "followers_count" = 259;
    id = 8404;
    industry = "<null>";
    "messages_count" = 1436;
    ric = "GRPN.O";
    sector = "<null>";
    symbol = GRPN;
    title = Groupon;
    "updated_at" = "2011-09-05T04:17:56Z";
}

How can I take these contents and put it into a new NSMutableDictionary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

无力看清 2024-12-10 08:15:58

使用-mutableCopy

NSDictionary *d;
NSMutableDictionary *m = [d mutableCopy];

请注意,-mutableCopy 返回id(Swift 中的Any),因此您需要分配/转换为正确的类型。它创建原始字典的浅表副本。

Use -mutableCopy.

NSDictionary *d;
NSMutableDictionary *m = [d mutableCopy];

Note that -mutableCopy returns id (Any in Swift) so you will want to assign / cast to the right type. It creates a shallow copy of the original dictionary.

他不在意 2024-12-10 08:15:58

如果是斯威夫特

var tempDic: NSMutableDictionary = yourDictionary.mutableCopy() as NSMutableDictionary

In case of Swift

var tempDic: NSMutableDictionary = yourDictionary.mutableCopy() as NSMutableDictionary
[浮城] 2024-12-10 08:15:58

在 swift 3 中试试这个

let mutableDic: NSMutableDictionary =  (yourDictionary as! NSDictionary).mutableCopy() as! NSMutableDictionary

Try this in swift 3

let mutableDic: NSMutableDictionary =  (yourDictionary as! NSDictionary).mutableCopy() as! NSMutableDictionary
很糊涂小朋友 2024-12-10 08:15:58
NSMutableDictionary *newInfo = [[NSMutableDictionary alloc] init];
[newInfo setDictionary:info];

也有效,因为您将字典设置为不附加。您还可以使用:

[newInfo addEntriesFromDictionary:info];

获得相同的效果并附加到现有内容。

NSMutableDictionary *newInfo = [[NSMutableDictionary alloc] init];
[newInfo setDictionary:info];

Also works, since you are setting the dictionary not appending. You can also use:

[newInfo addEntriesFromDictionary:info];

for the same effect and to append to existing content.

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