crash while removing objects from NSMutableArray
Asked Answered
R

6

17

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

Refection answered 10/5, 2013 at 13:21 Comment(6)
Where is surveys declared and initialized? From the crash log, it looks like it's a NSArray not NSMutableArray.Tocology
Why are you removing all of the objects from the array if you are just going to replace it with a different array? Just release the array then create a new one.Ferrol
@Ferrol i am in ARC enabled environment,, how do i release a object?Refection
@RaviKiran Then you don't have to release it explicitly, just set a new array.Ferrol
you might want to read up on ARC, and what does it do for you. NSMutableArray's removeObject has nothing to do with releasing an object.Colson
i know we no need to release it explicitly,Refection
K
19

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

Karmenkarna answered 10/5, 2013 at 13:36 Comment(5)
ya in one place i am assigning NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,nil]; surveys = (NSMutableArray *) [[[NSMutableArray alloc]initWithArray:surveys ] sortedArrayUsingDescriptors:sortDescriptors]; i guess this is causing that issue ,,how can i resolve this?Refection
@RaviKiran If you surveys is a mutableArray [surveys sortUsingDescriptors:sortDescriptors]; would do fine.Dyne
@RaviKiran I have given an answer optimizing the sorting :)Dyne
` surveys = (NSMutableArray *) [[[NSMutableArray alloc]initWithArray:surveys ] sortedArrayUsingDescriptors:sortDescriptors];` in this line though you have typecasted. The object at the memory location is an NSArray object. Should never do that.Karmenkarna
The answer really was simple, thank you! In my case I was assigning NSArray *newArray = someArray.copy; instead of NSArray *newArray = someArray.mutableCopy;Cynthy
M
9

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.

Marquittamarr answered 10/5, 2013 at 13:29 Comment(0)
C
7

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.

Cene answered 10/5, 2013 at 13:24 Comment(2)
i have tried that,,, its crashing by throwing the exception [__NSArrayI removeObject:]: unrecognized selector sent to instance 0xa391640Refection
If you want to add objects in your NSMutableArray that is found in an NSArray, just do a for loop inside that NSArray and add it one by one to your NSMutableArray. That way, you can removeAllObjects inside your NSMutableArray. Do not directly assign your NSArray to that NSMutableArray since that makes it an array that isn't mutable anymore.Spermatium
N
1

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

Noeminoesis answered 10/5, 2013 at 13:28 Comment(0)
D
0

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]];
Dyne answered 10/5, 2013 at 14:8 Comment(0)
W
0

You might have initialised with Immutable Array. Use the following Code:

NSMutableArray *newArray = [NSMutableArray arrayWithArray: oldArray];

Wendelin answered 5/2, 2014 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.