0

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

1
  • what values in arr1, NSLog it? Commented Feb 12, 2013 at 8:42

2 Answers 2

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

Use these lines.

Sign up to request clarification or add additional context in comments.

4 Comments

Here id has no data type so we use this for removing & adding at new index in array...
But I dont know the index of that Question Id.How can I find it and the compare.?
First I have to check whether the same questionId exists or not .If exists then replace
Ok see this link may it helps you in your work: stackoverflow.com/questions/10457527/…
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.