繁体   English   中英

如何在iPhone中比较NSString值和NSMutableDictionary键?

[英]How to compare NSString value with NSMutableDictionary key in iPhone?

我是开发新手。 我不明白为什么我的代码不能正常工作

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

其中selectAnswer是NSString ,而correct_answer是'key',0是'value'。 请告诉我为什么它不会进入“如果”身体的解决方案?

试试这段代码:

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

   }

您正在比较字符串,因此您必须使用isEqualToString代替“==”。

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

希望这可能对你有所帮助。

实际上你现在正在comparing两个pointersaddress of string )而不是两个strings 所以使用不同朋友提供的ans喜欢:

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

要么

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM