Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 931
Ravi Kiran Avatar asked May 10 '13 13:05

Ravi Kiran


3 Answers

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

like image 186
devluv Avatar answered Oct 20 '22 20:10

devluv


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.

like image 9
Michał Ciuba Avatar answered Oct 20 '22 20:10

Michał Ciuba


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.

like image 7
Thilina Chamath Hewagama Avatar answered Oct 20 '22 22:10

Thilina Chamath Hewagama