So for a while I have been working on this app - in which contains a folder-like structure of objects which have properties of arrays that can contain the same type of object, which in turn can contain more of this object, etc. These objects are called groups. In a shared data class I have a mater group object called mainGroup, and a property of my data class called selectedGroup to facilitate the display of data. These group objects can be modified, so for a considerable chunk of time I tried to develop a solution for getting the path through all of the arrays to the desired object, all the while having no clue about copying and that assigning one array equal to the other does not create a deep copy, just some kind of reference. The code was logically sound, but in it after a while I noticed that deleting things in the selectedGroup’s array property had the same effect on the actual master copy, which did some wonky things to my app.
I did some googling and figured out that I was not actually creating a whole new array, just referencing it some how. I’m still confused on this next part though. I thought that setting one array equal to the other would have the same effect as calling copy on it - and when I replace or delete objects in one array the other is affected. So what exactly is the compiler doing when you set two arrays equal to each other? Shouldn’t it just populate the new array with the pointers to the objects in the old array, not just reference the array itself? And how come that when doing this same operation with objects such as NSStrings you do not see the same effect? Lastly, what other objects have this same behavior as the NSArrays?
Aucun commentaire:
Enregistrer un commentaire