rakeshNS
rakeshNS

Reputation: 4257

NSMutableArray automatically typecasting into NSArray

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....

Upvotes: 2

Views: 515

Answers (6)

lawicko
lawicko

Reputation: 7344

There is a mistake in your understanding of how Objective-C works. This line:

NSMutableArray *array = [[NSMutableArray alloc]init];

allocates and initializes the array, and the pointer array points to this object. Now, this line:

array = [myString componentsSeperatedByString:@"++"];

makes the array pointer to point to the new array returned by componentsSeparatedByString method. 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.

Upvotes: 6

highlycaffeinated
highlycaffeinated

Reputation: 19867

On the line

array = [myString componentsSeperatedByString:@"++"];

you are replacing the NSMutableArray you allocated with a new NSArray (and leaking your NSMutableArray. Try using this:

NSMutableArray *array = [NSMutableArray arrayWithArray:[myString componentsSeperatedByString:@"++"]];

Upvotes: 1

Felix
Felix

Reputation: 35384

An NSMutableArray instance won't be converted automatically.

The method componentsSeperatedByString returns an NSArray-Object. You should get a compiler warning when assigning the return-value to a NSMutableArray-Pointer.

Upvotes: 0

basvk
basvk

Reputation: 4546

This is because – componentsSeparatedByString: returns a NSArray: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/componentsSeparatedByString:

Do something like:

array = [NSMutableArray arrayWithArray:[myString componentsSeperatedByString:@"++"]];

Upvotes: 1

A-Live
A-Live

Reputation: 8944

It's not converted, the array pointer could be UIButton* if you write something like

array = [self likeThatButton];

where likeThatButton is your method returning UIButton*. As always in objective c, NSMutableArray *array means only the Xcode will try to analyze your code and suggest convenient warnings and code completion.

Upvotes: 0

Alladinian
Alladinian

Reputation: 35626

This is happening because [myString componentsSeperatedByString:@"++"] returns an NSArray. You can try something like this:

array = [[myString componentsSeperatedByString:@"++"] mutableCopy];

or

array = [NSMutableArray arrayWithArray:[myString componentsSeperatedByString:@"++"]];

Upvotes: 4

Related Questions