I created an NSMutableArray object by
NSMutableArray *array = [[NSMutableArray alloc]init];
and used method componentsSeperatedByString: as
array = [myString componentsSeperatedByString:@"++"];
but when I performed operation on array like,
[array removeAllObjects];
I got exception like “removeAllObjects unrecognized selector send to instance”.
I solved this issue by modifying code like,
NSArray *components = [myString componentsSeperatedByString:@"++"];
array = [NSMutableArray arrayWithArray:components];
and I after that could perform operation like
[array removeAllObjects];
My doubt is why did NSMutableArray automaticaqlly converted to NSArray? How Can I avoid automatic type conversion like this, to prevent exceptions? Thanks in advance….
There is a mistake in your understanding of how Objective-C works. This line:
allocates and initializes the array, and the pointer
arraypoints to this object. Now, this line:makes the
arraypointer to point to the new array returned bycomponentsSeparatedByStringmethod. You loose the reference to your alloced and inited mutable array when you do this, and you create the memory leak if you don’t use ARC.