How can I convert an NSDictionary to an NSMutableDictionary?

戏子无情 提交于 2019-11-27 12:41:12

问题


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?


回答1:


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.




回答2:


In case of Swift

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



回答3:


Try this in swift 3

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


来源:https://stackoverflow.com/questions/7313524/how-can-i-convert-an-nsdictionary-to-an-nsmutabledictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!