2
votes

I am trying to add object to the array from a dictionary . In else part I am getting this error

mutating method sent to immutable object'

NSMutableDictionary *selectedDict = [NSMutableDictionary new];
    [selectedDict setObject:editedLineItem forKey:@"Text"];
    [selectedDict setObject:@"fa-check" forKey:@"IconClass"];
    NSMutableArray *tagListDictionary = [NSMutableArray new];
    [tagListDictionary addObject:selectedTagsArray];
    LineItemsStorage *linestorage = [LineItemsStorage sharedManager];
    if(![linestorage.packagesArray valueForKey:@"Id"])
    {
        [linestorage.selectedLineItemsAndTagsArray addObject:selectedDict];
    }
    else{        [[linestorage.packagesArray valueForKey:@"LineItems"]addObject:[NSMutableArray arrayWithObject:selectedDict]];
    }

-[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object' *** First throw call stack: ( 0 CoreFoundation 0x00000001154a1d85 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000114f15deb objc_exception_throw + 48 2 CoreFoundation 0x00000001154a1cbd +[NSException raise:format:] + 205 3 CoreFoundation 0x0000000115497b0a -[__NSCFArray insertObject:atIndex:] + 106 4 FlatPebble 0x000000010f276014 -[LineItemViewController okayAction] + 836 5 UIKit 0x0000000113809a8d -[UIApplication sendAction:to:from:forEvent:] + 92 6 UIKit 0x000000011397ce67 -[UIControl sendAction:to:forEvent:] + 67 7 UIKit 0x000000011397d143 -[UIControl _sendActionsForEvents:withEvent:] + 327 8 UIKit 0x000000011397c263 -[UIControl touchesEnded:withEvent:] + 601 9 UIKit 0x000000011387c99f -[UIWindow _sendTouchesForEvent:] + 835 10 UIKit 0x000000011387d6d4 -[UIWindow sendEvent:] + 865 11 UIKit 0x0000000113828dc6 -[UIApplication sendEvent:] + 263 12 UIKit 0x0000000113802553 _UIApplicationHandleEventQueue + 6660 13 CoreFoundation 0x00000001153c7301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 14 CoreFoundation 0x00000001153bd22c __CFRunLoopDoSources0 + 556 15 CoreFoundation 0x00000001153bc6e3 __CFRunLoopRun + 867 16 CoreFoundation 0x00000001153bc0f8 CFRunLoopRunSpecific + 488 17 GraphicsServices 0x0000000116e5cad2 GSEventRunModal + 161 18 UIKit 0x0000000113807f09 UIApplicationMain + 171 19 *********** 0x000000010f348c2f main + 111 20 libdyld.dylib 0x0000000115d9992d start + 1 )

2
I suspect that the object for key LineItems is immutable and do not use valueForKey: unless you really really need KVC. - vadian
Only use valueForKey: if you can explain to a code reviewer why you use valueForKey: and not objectForKey: - gnasher729
In packages Array , there are two objects at which I must send these values to linestorage.packagesArray - Chintu the cool kid
Mostly likely valueForKey returns immutable array which you are trying to add object to. - Rob Zombie
Your problem is that you're adding an object to lineStorage.selectedLineItemsAndTagsArray (which is an immutable NSArray and not a NSMutableArray). This is your key: [NSCFArray insertObject:atIndex:]: mutating method sent to immutable object. It says you're sending an insertObject:atIndex: method to a NSArray and it should be a NSMutableArray. Remember to use the Mutable subclasses if you pretend to modify your objects or their contents. Change your selectedLineItemsAndTagsArray property to be a NSMutableArray and you should be fine (probably your packagesArray too) - Alejandro Iván

2 Answers

0
votes

Use this code

 NSMutableDictionary *selectedDict = [[NSMutableDictionary new]mutableCopy];
    [selectedDict setObject:editedLineItem forKey:@"Text"];
    [selectedDict setObject:@"fa-check" forKey:@"IconClass"];
    NSMutableArray *tagListDictionary = [[NSMutableArray new]mutableCopy];
    [tagListDictionary addObject:selectedTagsArray];
    LineItemsStorage *linestorage = [LineItemsStorage sharedManager];
    if(![linestorage.packagesArray valueForKey:@"Id"])
    {
        [linestorage.selectedLineItemsAndTagsArray addObject:selectedDict];
    }
    else{        [[linestorage.packagesArray valueForKey:@"LineItems"]addObject:[NSMutableArray arrayWithObject:selectedDict]];
    }
0
votes

Assuming your packagesArray contains an array containing one or more objects that have a LineItems property (that happens to also be an array); your problem is here:

[[linestorage.packagesArray valueForKey:@"LineItems"]addObject:[NSMutableArray arrayWithObject:selectedDict]]

Breaking it down it is equivalent to:

 NSArray* packagesLineItems = [linestorage.packagesArray valueForKey:@"LineItems"];
 NSMutableArray* selected = [NSMutableArray arrayWithObject:selectedDict];
 [packagesLineItems addObject:selected];

So your problem is either the return type of valueForKey (when called on an array), or the action you're trying to do on it.