zeeshan shaikh
zeeshan shaikh

Reputation: 819

How to compare NSString value with NSMutableDictionary key in iPhone?

I'm new in development. I don't understand why its not working my code is below

if(selectAnswer == [[self.testQuesArray objectAtIndex:0]objectForKey:@"correct_answer"]
{
    NSLog(@"Correct Answer");
}

Where selectAnswer is NSString, and correct_answer a 'key' and 0 is the 'value' of this. Please tell me the solution why its not coming in the "If" body ?

Upvotes: 0

Views: 848

Answers (3)

Prateek Prem
Prateek Prem

Reputation: 1544

Actually right now you are comparing two pointers(address of string) not two strings. So Use the ans provided by different friends Like:

if([selectAnswer isEqualToString : [self.testQuesArray[0][@"correct_answer"] stringValue])

or

if([selectAnswer isEqualToString : [NSString stringWithFormat:@"%@",[[self.testQuesArray objectAtIndex:0]objectForKey:@"correct_answer"]]]

Upvotes: 0

Ponting
Ponting

Reputation: 2246

You are comparing string so you have to use isEqualToString in place of "==".

if([selectAnswer isEqualToString : [NSString stringWithFormat:@"%@",[[self.testQuesArray objectAtIndex:0]objectForKey:@"correct_answer"]]]
{
NSLog(@"Correct Answer");
}

Hope this might help you.

Upvotes: 1

kirti Chavda
kirti Chavda

Reputation: 3015

try this code:

 if ([selectAnswer isEqualToString:[NSString stringWithFormat:@"%@",[[self.testQuesArray objectAtIndex:0]objectForKey:@"correct_answer"]]]) 
   {

   }

Upvotes: 1

Related Questions