Remove an object in NSMutableArray? - iphone

I have a NSMutableArray like this:
NSMutableArray *array =...; (array contain my custom object)
NSMutableArray *toRemove = [array getObjectsToRemove];
But when I try to call removeObjectsInArray ( [array removeObjectsInArray:toRemove]; ) it breaks my app! Can anybody tell me how to solve my problem :(

I expect the error you are getting is "[NSMutableArray getObjectsToRemove] unrecognized selector sent to.."
NSMutableArray does not have such a method.
That's as far as I can answer without a bit more information, like the actual error message and the code for getObjectsToRemove (wherever it is defined).

Related

crash while removing objects from NSMutableArray

In my iphone project (ARC enabled) i have a nsmuatble array which contains some 5 managed objects (which are retrieved from core data ) and in some scenario i need to remove all the objects from that nsmutablearray
i have used following methods to remove objects but its crashing in both the cases with the crash log -[__NSArrayI removeObject:]: unrecognized selector sent to instance 0xa391640
if (surveys && [surveys count]>0)
{
[surveys removeAllObjects];
surveys = [[NSMutableArray alloc]init];
}
and also i tried
if (surveys && [surveys count]>0)
{
for(Survey *obj_Survey in surveys)
{
[surveys removeObject:obj_Survey];
}
surveys = [[NSMutableArray alloc]init];
}
can any one tell me how do i empty that array,, any suggestions would be appreciated, thanx in advance
The answer is very simple.
For First case.
Check the part where you have initialized your array, and check if somewhere through your code, if you have assigned an NSArray to your NSMutableArray object.
For second case.
You cannot remove objects from the array while you are enumerating through it. This will result in a crash saying array has mutated while enumerating
This:
[__NSArrayI removeObject:] : unrecognized selector sent to instance 0xa391640
indicates that your array "surveys" is NSArray (not-mutable). Are you sure you have initialized it properly? If you do it by an assignment like this
NSMutableArray* surveys = [someManagedObjectContext executeFetchRequest:request]
an array "surveys" will be of type NSArray because Core Data fetch requests return NSArrays.
The error message says it all,
[__NSArrayI removeObject:]: unrecognized selector
__NSArrayI is a code-word for an immutable array (NSArray), That means your surveys object is not NSMutablArray, but NSArray.
You cannot add or remove objects from NSArray.
Check your code. You have assigned NSArray to surveys or you have reinitialized it as NSArray.
Remove all the things that you are doing and simply do like this
[surveys removeAllObjects];
As from the error that you are getting so maybe that Array is not NSmutableArray just normal array so thats why you can not remove object from It and you try to do so and app got crash
check where you have initialised it if it is Mutable or not
so as I said remove all that thing and use simple removeAllObject
From the exception it's very evident that at some point you are assigning an immutable instance to your mutable array. From the comments I could make out that you were sorting surveys and sorted array was returning immutable array. Type casting will not do good for getting mutableCopy of an immutable object.
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,nil];
surveys = (NSMutableArray *) [[[NSMutableArray alloc]initWithArray:surveys ] sortedArrayUsingDescriptors:sortDescriptors];
Instead of it use
[surveys sortUsingDescriptors: #[sortDescriptor1]];
You might have initialised with Immutable Array.
Use the following Code:
NSMutableArray *newArray = [NSMutableArray arrayWithArray: oldArray];

Objective c add string object to an array

I've looked everywhere for this, online, on stack overflow and cannot still work out what I'm doing wrong.
I'm trying to add an element to an existing NSMutableArray. But it crashes on line 4:
-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x897b320
The code:
NSMutableArray *mystr = [[NSMutableArray alloc] init];
mystr = [NSArray arrayWithObjects:#"hello",#"world",#"etc",nil];
NSString *obj = #"hiagain";
[mystr addObject:obj];
What am I doing wrong? This is driving me crazy!!!
You array is not mutable!. Use NSMutableArray
mystr = [NSMutableArray arrayWithObjects:#"hello",#"world",#"etc",nil];
You get unrecognized selector since NSArray does not contain the addObject method
Your code should be:
NSMutableArray *mystr = [[NSMutableArray alloc] initWithObjects:#"hello",#"world",#"etc",nil];
NSString *obj = #"hiagain";
[mystr addObject:obj];
The second line you're reassigning an instance of NSArray rather of NSMutableArray to your mystr variable.
Try something like this:
NSMutableArray *mystr = [NSMutableArray arrayWithObjects:#"hello",#"world",#"etc",nil];
[mystr addObject:#"hiagain"]
You originally create mystr as a mutable array, but then assign it to a standard NSArray in the next line. Instead of calling "arrayWithObjects," add each item using "addObjects" or some other function that doesn't create a new immutable array.
Ahhhhh spotted it already
Line 2 should be:
[NSMutableArray arrayWithObjects:#"hello",#"world",#"etc",nil];
Sorry to waste your time with that!

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....
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.
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:#"++"]];
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:#"++"]];
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:#"++"]];
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.
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.

How do i change a data using NSMutableDictionary?

I have an NSMutableArray named as totalunits. It has some data. Each data has value like this. ({blah blah blah},{blah = 1}).
The second value should change to 0 instead of 1. Thats what I explained in following codes.
NSMutableDictionary *inappDict=[[NSMutableDictionary alloc] init];
[inappDict setObject:#"0" forKey:#"inapp"];
[[totalunits objectAtIndex:currenttag] replaceObjectAtIndex:1 withObject:newDict];
But using this, I'm getting an exception like this:
[__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent
to immutable object.
Help me.
Thanks in advance
You've initialized your NSMutableArray as NSArray, so it has no mutation methods; double check, that you need to operation NSMutableArray and not NSArray

Populate UITableView using NSMutableDictionary, keysSortedByValueUsingSelector

My model class has a NSMutableDictionary with my data. In my view class, I want to display that data in my UITableView cellforRowAtIndexPath method. My view class has an NSArray for the data to display.
I can do this to get my data into my NSArray to display correctly:
self.CategoriesArray = [model.CategoryDictionary allKeys];
However, since NSMutableDictionary does not sort on its own, my items in my TableView are not in alphabetical order. I thought I could do this:
However, I get the following error:
2Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFBoolean localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance 0x13d0a20'
I'm assuming that's because my view class doesn't know what to do with the localizedCaseInsensitiveCompare method.
How do I solve this problem? Thanks.
I think you were saying that this gives you the correct results, just not in order, right?
self.CategoriesArray = [model.CategoryDictionary allKeys];
If that's the case, then this will give you the correct order if I'm understanding your question correctly:
NSArray* keys = [model.CategoryDictionary allKeys];
self.CategoriesArray = [keys sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
localizedCaseInsensitiveCompare: is only to compare NSString, not NSDictionary or NSArray.
To sort your categoriesArray look at
sortedArrayUsingComparator: and sortedArrayUsingDescriptors: for NSArray
sortUsingDescriptors and sortUsingComparator for NSMutableArray
To loop through your array you can do something like
for (NSString *string in categoriesArray)
{
}