views:

53

answers:

3

i use this code to check if any objects exist in my NSMutableArray if yes i remove them all but it crashes although there are objects why?

    if([NSMutableArray1 count]==1)
    {
        [poemoptionslist removeAllObjects];
    }



    if ([NSMutableArray1 count]==0)
    {
        [poemoptionslist addObject: final1];
    }

CONSOLE OUTPUT

2010-10-18 03:42:13.166 app1[33398:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object' * Call stack at first throw: ( 0 CoreFoundation
0x02e55b99 exceptionPreprocess + 185 1 libobjc.A.dylib
0x02fa540e objc_exception_throw + 47 2 CoreFoundation
0x02e0e238 +[NSException raise:format:arguments:] + 136 3
CoreFoundation
0x02e0e1aa +[NSException raise:format:] + 58 4
CoreFoundation
0x02e4d3c1 -[__NSCFArray removeObjectAtIndex:] + 193 5
CoreFoundation
0x02dfe973 -[NSMutableArray removeAllObjects] + 83 6
poemsoflove
0x0004dc8d -[submitpoem submitpoem:] + 18560 7 UIKit
0x003b77f8 -[UIApplication sendAction:to:from:forEvent:] + 119 8 UIKit
0x00442de0 -[UIControl sendAction:to:forEvent:] + 67 9
UIKit
0x00445262 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 10 UIKit
0x00443e0f -[UIControl touchesEnded:withEvent:] + 458 11 UIKit
0x003db3d0 -[UIWindow _sendTouchesForEvent:] + 567 12 UIKit
0x003bccb4 -[UIApplication sendEvent:] + 447 13 UIKit 0x003c19bf _UIApplicationHandleEvent + 7672 14 GraphicsServices
0x033e6822 PurpleEventCallback + 1550 15 CoreFoundation
0x02e36ff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION
+ 52 16 CoreFoundation 0x02d97807 __CFRunLoopDoSource1 + 215 17 CoreFoundation
0x02d94a93 __CFRunLoopRun + 979 18 CoreFoundation
0x02d94350 CFRunLoopRunSpecific + 208 19 CoreFoundation
0x02d94271 CFRunLoopRunInMode + 97 20 GraphicsServices
0x033e500c GSEventRunModal + 217 21 GraphicsServices
0x033e50d1 GSEventRun + 115 22 UIKit 0x003c5af2 UIApplicationMain + 1160 23 poemsoflove
0x00002728 main + 102 24 poemsoflove 0x000026b9 start + 53 25 ???
0x00000001 0x0 + 1 ) terminate called after throwing an instance of 'NSException' Program received signal: “SIGABRT”.

Guys there is no NSArray!

I save to NSUSerdefaults like this:

if ([mutable1 count]==0) { [mutable1 addObject: final1]; }

    NSUserDefaults *list =[NSUserDefaults standardUserDefaults];
    [list setObject:mutable1 forKey:@"favorites"];
    [list synchronize];

and i load data like this

NSUserDefaults *prefs1 =[NSUserDefaults standardUserDefaults];

if ( [prefs1 objectForKey:@"favorites"] != nil)
{
    mutable1 = [[NSMutableArray alloc] init];
    mutable1 = [prefs1 objectForKey:@"favorites"];

and i get the objects! then when it runs the removeallobjects it crashes!

A: 

Somewhere you have set NSMutableArray1 to an instance of NSArray, not NSMutableArray or you declared NSMutableArray1 as a property of type NSArray vs. NSMutableArray.

Also, you should follow Cocoa / Objective-C naming conventions. Namely, class names start with upper case; variables take the form myArray1 (or something more descriptive, preferably).

Joshua Nozzi
A: 

The error messages indicate that you're sending the message to an immutable array, which raises an exception. Uncaught exceptions lead to program termination.

How are you creating the array? The most common error that can lead to this is doing something like:

[mutableArray copy]

Even if the thing you're copying is mutable, the copy will be immutable. In that randomly chosen example, use mutableCopy insted.

Tommy
+2  A: 
mutable1 = [[NSMutableArray alloc] init];
mutable1 = [prefs1 objectForKey:@"favorites"];

Even though you've declared mutable1 to be an NSMutableArray, you are reassigning it to the object returned by your NSUserDefaults object. This object is apparently an NSArray rather than an NSMutableArray, hence the crash.

You can load your NSMutableArray with the preferences array by doing something like this:

mutable1 = [[NSMutableArray alloc] init];
[mutable1 addObjectsFromArray:[prefs1 objectForKey:@"favorites"]];
highlycaffeinated
thank you so much i was seraching for hours about this problem!
stefanosn