reprint http://blog.sina.com.cn/s/blog_6b1e4a060102uz0i.html
| Tags:nsarraycopy__nsarrayiremoveobjectatindex pointer pointing |
Category: Learning Notes |
Long time no blog, see colleagues in the code used to copy this method, before also have learned, but not very deep, the concept is more vague, although has been engaged in a year of IOS development, but still do not understand really ashamed, so I wrote something to test, I know that iOS are pointers That is, if you have an array and then declare a array1 = [array copy], then array1 and array are pointing to the same piece of memory, so when you change the contents of the array1, then the array will naturally change, so there is the following code and appears The problem:
Nsmutablearray *array = [nsmutablearrayarraywithobjects:@ "111", @ "222", @ "333", nil]; Nsmutablearray *array1 = [array copy]; Nsmutablearray *array2 = array1; NSLog (@ "aray:%@,array1:%@,array2:%@", array,array1,array2); [Aray1 removeobjectatindex:1];NSLog (@ "aray:%@,array1:%@,array2:%@", array,array1,array2);According to our thinking, then the first print out should be all 111,222,333, yes, yes, so what's the second print? We also think that it should be array 111,222,333, and array1 and array2 should be 111,333, right? The answer is wrong, why, because the program run to [aray1removeobjectatindex:1]; error, then report what is wrong, why will the error?
-[__nsarrayi removeobjectatindex:]:unrecognized selector sent to instance this is a mistake,
According to our experience, this kind of error probably also does not have this method in the array or cannot respond to this method, how possibly, obviously is the Nsmutablearray type the how possibly does not have or cannot respond the Removeobjectatidex method,
Later I thought for a long time, may be copy of the problem, so I put nsmutablearray*array1 = [array copy]; Replace with Nsmutablearray *array1 = [Nsmutablearrayarraywitharray:array];
This will not be an error, the problem solved,
Later, I think, although the problem is solved, but this question is what place, why this, think for a long time I guess, may be although array1 we affirmed nsmutablearray but it is possible to copy after it became Nsarray before I met a similar question , so I went online to check the data carefully see how the copy is a matter, and then finally understand that copy is a shallow copy, although the declaration is nsmutablearray but after the copy of the array is still immutable, if you want to let him variable so [array copy]; [Arraymutablecopy]; Yes, sure enough. I solved the problem immediately, and the result was the same as we expected,
Through this question, summed up as follows: Copy is a shallow copy, although declared as nsmutable variable, the result is still immutable, if you want to make the variable after the copy, then use Mutablecopy.
Although the development of a year iOS but the basic things are not too clear, really ashamed, and, if you use NSString to verify the pointer to the problem, it seems not possible, because nsstring *string = @ "123"; this method and nsstring*string = [[ NSString alloc]initwithstring:@ "123"]; it seems to be equivalent (this is my own guess, I have used string to verify, who makes string so simple and convenient, everyone began to naturally think of using string, I am no exception, each time will be new out a piece of memory, so that does not achieve the effect we want, so if you want to use NSString to verify, string initialization is best used [[NSString alloc]initwithstring:@ "123"]; This should be possible.
"Go" nsarray copy issue