2011-11-01 53 views
1

Possible Duplicate:
How to compare strings? (== returns wrong value)如何檢查UILabel中的文本?

我有以下的代碼,但沒有任何反應,這是爲什麼?:

if(Assignment1DueDate.text == @"0 days until due"){ 

     [Assignment1DueDate setText:@"Due tomorrow"]; 

     [maincelltext addObject:Assignment1.text]; 
     [subtitlecelltext removeObject:Assignment1DueDate.text]; 
     [subtitlecelltext addObject:Assignment1DueDate.text]; 

} 

附: Assignment1DueDate是UILabel。

回答

11
if([Assignment1DueDate.text isEqualToString:@"0 days until due"]){ 

// do your stuff 
} 
1

使用isEqualToString方法比較2個字符串。使用'=='運算符不能比較字符串。

+0

請寫評論來證明你的倒票 – Naved

0
if([Assignment1DueDate.text isEqualToString :@"0 days until due"]) 
{ 
} 

,如果你是比較字符串,那麼你必須使用isEqualToString.If你比較,你必須使用功能的isEqual兩個對象

0

下面的代碼是用於兩個CASE_INSENSITIVE比較字符串。

UILabel *labl = [[UILabel alloc] initWithFrame: CGRectMake(50, 50, 100, 80)]; 
labl.text = @"my Label"; 
[self.view addSubview:labl]; 


if ([ labl.text caseInsensitiveCompare:@"MY LABel"]==NSOrderedSame) 
{ 
    // Code to be executed if Assignment1DueDate is equal to the entered String. 
    NSLog(@"Label and String are Same"); 
} 
else 
{ 
    // Code to be executed if Assignment1DueDate is Not equal to the entered String. 
    NSLog(@"Label and String are Not Same"); 
} 

上面的代碼返回「標籤和字​​符串相同」。 此代碼工作正常,我已測試它。希望它能幫助你。

+0

我喜歡這個答案的想法,但我得到這個錯誤:http://pastie.org/2794225 - 出了什麼問題? :S – pixelbitlabs

+0

嗨reddexuk你必須先拿一個標籤並設置文本。之後,將其與字符串進行比較。 –