开发者

How can I convert an NSDictionary to an NSMutableDictionary?

I have an existing NSDictionary that has:

{
    "charts_count" = 2;
    "creat开发者_Python百科ed_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?


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.


In case of Swift

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


Try this in swift 3

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜