nsmutabledictionary - When to and when not to use (mutable) copies in assignments (Objective C) -


i new objective c , had no idea in ns(mutable)dictionary must use (mutable)copy assignment this:

dict[@"backup"] = dict[@"mylist"];

using debugging found out assignment must done this:

dict[@"backup"] = [dict[@"mylist"] mutablecopy];

now question is: how know must use copies (vs references) , type of objects?

thank you!

there nothing must do.

a dictionary contains key-value pairs. example, code work, dict contains object value key "mylist". no idea object is. can make 3 different assignments, , each valid different:

dict [@"backup"] = dict [@"mylist"]; 

stores same object there under key mylist under key backup well. if object mutable, , modifies object, object under each key modified, because same object.

dict [@"backup"] = [dict [@"mylist"] copy]; 

"copy" interesting. create copy of object, have 2 objects, old 1 , new one. if original mutable, copy immutable. if original immutable, os assumes there no point in making copy, copy give original object. anyway, dict [@"backup"] immutable object cannot affected modifications dict [@"mylist"], either because not same object, or because dict [@"mylist"] cannot modified.

dict [@"backup"] = [dict [@"mylist"] mutablecopy]; 

this makes mutable copy of original , stores it. not same object. , can modified.

it depends on want achieve. there no right or wrong here.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -