Honey
Honey

Reputation: 2880

removeobject from nsmutablearray

I m doing a quiz application in which I have stored the correctanswer and its question Id in a NSMutableArray.I m displaying it in a section tableView.Now if I mark another answer in the same section then the prevoius have to be removed and this one has to be attached to the array. But Im unable to get it .

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

But isTheObjectThere is always 'NO' eventhough there is same question Id.

How can I remove prevoius one and replace by the new one

Upvotes: 0

Views: 99

Answers (2)

Ravindra Bagale
Ravindra Bagale

Reputation: 17655

use replaceObjectAtIndex:withObject:. just replace your answer.like this

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

Upvotes: 0

Vishal
Vishal

Reputation: 8256

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

Use these lines.

Upvotes: 1

Related Questions