0

I run a for to choose which objects to remove. After i removed one the app crashes. I´ve read that i couldn´t remove an object in the for because of the index, so i remove them outside but it still crashes. When it crashes at runtime it says the array it is an NSArray which is not, it is a NSMutablearray. Anyone knows why is this happening?

Here is the code:

NSMutableArray *discardedItems = [NSMutableArray array];
NSInteger catalogo = [[tmpDic objectForKey:@"catalogo"] intValue];
NSInteger documento = [[tmpDic objectForKey:@"documento"] intValue];

for (i=0; i<[self.deletedDocuments count] ; i++) 
{
    if ([[[self.deletedDocuments objectAtIndex:i] objectForKey:@"documento"] intValue] == documento &&
        [[[self.deletedDocuments objectAtIndex:i] objectForKey:@"catalogo"] intValue] == catalogo) 
        [discardedItems addObject:[self.deletedDocuments objectAtIndex:i]];
}

NSLog(@"Dictionary\n%@", self.deletedDocuments);
if (discardedItems != nil)
    [self.deletedDocuments removeObjectsInArray:discardedItems];
bruno
  • 2,154
  • 5
  • 39
  • 64

3 Answers3

2

are you storing the array in user defaults? objects taken from user defaults will be immutable, even if a mutable subclass was stored

wattson12
  • 11,176
  • 2
  • 32
  • 34
  • Yes, i am... So that is the problem. So, now how do i save them? And be able to remove them? – bruno May 30 '12 at 16:18
  • 1
    make mutableCopy perhaps? http://stackoverflow.com/questions/2002174/copy-mutablecopy – ader May 30 '12 at 16:21
  • 1
    @ade is right, just do something like [[[NSUserDefaults standardUserDefaults] objectForKey:@"SomeKey"] mutableCopy]; – wattson12 May 31 '12 at 08:52
0

When it crashes at runtime it says the array it is an NSArray which is not, it is a NSMutablearray.

No, if the runtime says its an NSArray, it's an NSArray. Make sure you don't assign an NSArray to NSMutableArray * deletedDocuments variable. Note that it's deletedDocuments that is NSArray, not discardedItems.

MByD
  • 135,866
  • 28
  • 264
  • 277
0

try:

for (int i=[self.deletedDocuments count]-1; i>=0 ; i--) 
{
    if ([[[self.deletedDocuments objectAtIndex:i] objectForKey:@"documento"] intValue] == documento &&
    [[[self.deletedDocuments objectAtIndex:i] objectForKey:@"catalogo"] intValue] == catalogo) {
        [self.deletedDocuments removeObjectAtIndex:i];
}
}
ader
  • 5,403
  • 1
  • 21
  • 26