NSMutablearray崩溃删除对象

时间:2012-05-30 15:49:30

标签: ios crash nsmutablearray nsarray

我运行for来选择要删除的对象。我删除了一个应用程序崩溃后。 我读过因为索引我无法移除for中的对象,所以我将它们移除到外面但它仍然崩溃。当它在运行时崩溃时它表示数组是NSArray而不是,它是NSMutablearray。任何人都知道为什么会这样?

以下是代码:

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];

3 个答案:

答案 0 :(得分:2)

您是否将数组存储在用户默认值中?从用户默认值中获取的对象将是不可变的,即使存储了可变子类

答案 1 :(得分:0)

  

当它在运行时崩溃时它表示数组是NSArray而不是,它是NSMutablearray。

不,如果运行时说它是NSArray,则它是NSArray。确保不将NSArray分配给NSMutableArray * deletedDocuments变量。请注意deletedDocumentsNSArray,而不是discardedItems

答案 2 :(得分:0)

尝试:

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];
}
}