簡體   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