How do i change a data using NSMutableDictionary? - ios5

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

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];

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.

Need to use an array in another class.. error is arrayXYZ undeclared!

i have a class abcParser in which i have an array arrayXYZ. Now i need to use this array in a viewController.. when i do so it gives an error.. arrayXYZ undeclared
i m weak in inheritance, please help!
thanks!!
Declare a function in ABCParser that lets you retrieve the array, like so:
-(NSArray *)arrayXYZ:(NSString *)anArgument{
NSArray *array = [NSArray arrayWithObject:anArgument];
return array;
}
EDIT:
Alright, seems like we need a little lesson on instantiation etc...
Now, those Plus (+) and Minus (-) signs beside method names...
Plus is a class method. It can be used without instantiation, but understand that variables cannot be carried over when you use these.
Instance methods require you to instantiate a new object, then ask it to perform methods.
ABCParser *parser = [[ABCParser alloc] init];
That creates a new 'instance' of ABCParser and makes a pointer to it with the name 'parser'. That's all fine and dandy.
When we want this new parser object to do something, we tell it as normal:
NSArray *myNewArray = [parser arrayXYZ];
So - When you have an instance of your class, do this:
ABCParser *parser = [[ABCParser alloc] init];
[parser generateArrayXYZ];
NSArray *array = [parser getArrayXYZ];
This will give you an object that you can work with. It'll take a bit of getting used to, but you'll learn. If you need any further explanation, just post :)
Did you already add #property and #syntesize to your NSArray variable in abcParser?

Remove an object in NSMutableArray?

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

iPhone SDK: How to copy the contents of a NSArray to NSMutable array?

I need to copy the contents of an NSArray to NSMutable array. In other words, I want to copy arrayCountryChoices to arraySearchResults. Any ideas????
//main data
NSArray *arrayCountryChoices;
//search results buffer
NSMutableArray *arraySearchResults;
//create data
arrayCountryChoices = [[NSArray alloc] initWithObjects:#"foo",#"bar",#"baz",nil];
//copy the original array to searchable array ->> THIS IS NOT WORKING AS EXPECTED
arraySearchResults = [[NSMutableArray alloc] arrayWithArray:arrayCountryChoices];
Thanks in advance.
it's either
[NSMutableArray arrayWithArray:anArray];
or
[[NSMutableArray alloc] initWithArray:anArray];
or
[anArray mutableCopy];
The code in your example doesn't work because you're calling arrayWithArray on an instance of NSMutableArray, but arrayWithArray is a class method.
As a general rule, initalization methods that start with init are instance methods, and those that start with the name of the class (array, etc.) are class methods. Class methods return autoreleased objects, while instance methods return retained objects.
You can also create empty mutable array and add objects to it using -addObjectsFromArray method
Please note that when using:
[NSMutableArray arrayWithArray:anArray];
or
[[NSMutableArray alloc] initWithArray:anArray];
you'll always get an array even if anArray was nil, but with:
[anArray mutableCopy];
you'll get nil if anArray is nil. So if you want to be sure that you actually get an array even if it may be empty, use one the former methods.
It's not working because arrayWithArray is a class method, not an instance method.
You can do [NSMutableArray arrayWithArray:arrayCountryChoices] to do what you need.
You could just call [arrayCountryChoices mutableCopy] to return a new mutable array with the contents of arrayCountryChoices.