将NSdictionary对象强制转换为NSMutableDictionary是否可以?

Fas*_*sid 0 iphone objective-c

将NSdictionary对象强制转换为NSMutableDictionary是否可以?

NSDictionary *dict;
NSMutableDictionary *mDict = (NSMutableDictionary *)dict;
Run Code Online (Sandbox Code Playgroud)

试过这个,它工作得很好.

jer*_*jer 6

不它不是.如果你想要一个可变的字典,请制作一个:

NSMutableDictionary* mutableDict = [dict mutableCopy];
Run Code Online (Sandbox Code Playgroud)

不要忘记按照您拥有的规则,mutableDict并在完成后将其释放.

  • 您也可以轻松使用[NSMutableDictionary dictionaryWithDictionary:myOriginalDictionary]; 这将返回一个自动释放的对象. (2认同)