我怎么能转换一个NSDictionary到一个NSMutableDictionary?

问题描述 投票:41回答:3

我有一个现有的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?

iphone objective-c cocoa-touch nsdictionary nsmutabledictionary
3个回答
126
投票

使用-mutableCopy

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

需要注意的是-mutableCopy返回id(斯威夫特Any),所以你会想分配/强制转换为正确的类型。它创建原始字典的浅拷贝。


7
投票

在斯威夫特的情况下,

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

1
投票

在迅速3试试这个

let mutableDic: NSMutableDictionary =  (yourDictionary as! NSDictionary).mutableCopy() as! NSMutableDictionary
© www.soinside.com 2019 - 2024. All rights reserved.