Can you initialize an array with an array that may have been autoreleased?
-
27-10-2019 - |
Pergunta
I am trying to figure out an out of bound index issue i am getting in my newArray
object. I pass in oldArray
to the method and then use the following:
newArray = [[NSMutableArray alloc] initWithArray:oldArray];
If oldArray
has been released, would i get an error at this line, or would I get an out of bounds exception when I try to access newArray
?
Solução
No, the above code makes a copy of oldArray
into newArray
. Changing oldArray
in any way (including releasing it) will have no impact on newArray
, and in any case would certainly not cause a bounds exception.
The most likely cause is that oldArray
is nil
at this point, so newArray
is an empty array when you don't expect it to be. Alternately, oldArray
may not have as many elements in it as you think it does. Bounds exception means you're reading past the end of the array. It does not have anything to do with memory management.