Reputation: 819
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
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
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
Reputation: 3015
try this code:
if ([selectAnswer isEqualToString:[NSString stringWithFormat:@"%@",[[self.testQuesArray objectAtIndex:0]objectForKey:@"correct_answer"]]])
{
}
Upvotes: 1