从nsmutablearray中删除对象

时间:2013-02-12 07:19:31

标签: iphone nsmutablearray xcode4.5

我正在做一个测验应用程序,我在NSMutableArray中存储了correctanswer和它的问题ID。我在一个部分tableView.Now中显示它,如果我在同一部分标记另一个答案,那么必须删除prevoius这一个必须附加到数组。 但我无法得到它。

 NSMutableArray *cellSection = [self.finalarray objectAtIndex:indexPath.section];
    int n=[cellSection count];
    NSString *questionId=[[cellSection objectAtIndex:n-1]objectForKey:@"QId"];
 for (NSDictionary *dict in selectedOptionandQIdArray) {

        NSString *str=[dict valueForKey:@"QId"];

        [arr1 addObject:str];

    }

    BOOL isTheObjectThere = [arr1 containsObject:questionId];


    if(isTheObjectThere==YES)
    {
         //remove the prevoius answer and replace by the new answer for the same question id 
    }
 [selectedOptionsArray addObject:dictionary1];


    NSDictionary *dictinary2 =[[NSDictionary alloc] initWithObjectsAndKeys:questionId, @"QId",  nil];
    [selectedOptionsArray addObject:dictinary2];

    [selectedOptionandQIdArray addObject:selectedOptionsArray];

isTheObjectThere始终是“NO”,尽管存在相同的问题ID。

如何删除prevoius并替换为新的

2 个答案:

答案 0 :(得分:1)

id object = [[array objectAtIndex:0] retain];
[array removeObjectAtIndex:0];
[array insertObject:object atIndex:2];

使用这些行。

答案 1 :(得分:0)

使用replaceObjectAtIndex:withObject:。只需替换你的答案。就像这个

[selectedOptionsArray replaceObjectAtIndex:indexofrepacingElement withObject:@"newAnswer"];