I have and NSMutableArray and I want to replace it with another, but if I try to do it like this…
firstArray = secondArray;
…then it seems to erase the entire firstArray and I get this error message..
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
…and the bounds should be (6) not (0).
Is there a correct way to replace the array?
PS: I already checked the secondArray and it functions fine.
You’re assigning the firstArray pointer to that of the secondArray pointer, so you lose the reference to the firstArray object, and it gets leaked. If you want to replace the objects in the firstArray object, use something like
-replaceObjectsInRange:withObjectsFromArray:or just-releasethe firstArray object and assign firstArray to[secondArray mutableCopy].I recommend reading up on C pointers as well as Objective-C memory management rules to make sure you’ve got a firm grasp on the fundamentals.