2012-08-29 21 views
1

嗯,這是我的代碼:isEqualToString誤會我的意思結果

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    NSLog(@"Row %i at View: %@",indexPath.row, [delegate.my_condition objectAtIndex:indexPath.row]); 
    if ([@"0" isEqualToString:[delegate.my_condition objectAtIndex:indexPath.row]]){ 
     NSLog(@"Store is closed"); 
    } else { 
     NSLog(@"Store is open"); 

,並在日誌中我看到:

2012-08-29 22:33:59.434 MyApp[2593:c07] Row 0 at View: 0 
2012-08-29 22:33:59.435 MyApp[2593:c07] Store is open 
2012-08-29 22:33:59.437 MyApp[2593:c07] Row 1 at View: 0 
2012-08-29 22:33:59.438 MyApp[2593:c07] Store is open 
2012-08-29 22:33:59.440 MyApp[2593:c07] Row 2 at View: 0 
2012-08-29 22:33:59.441 MyApp[2593:c07] Store is open 

我確實認爲這個值是0,但我沒有看到,如果子句,但是else子句。

這是爲什麼?

myCondition is NSMutableArray 

這是我如何將項目添加到我的NSMutableArray:

NSString *parsed=[res objectForKey:@"Data"]; 
    [delegate.my_condition addObject:parsed]; 
+0

myCondition中有哪些類型的對象? –

回答

0

你肯定在delegate.my_condition的值是字符串?那些看起來像他們可能是NSNumber* s。當然NSNumber*將永遠不會等於NSString*

你可能想嘗試像

if ([[delegate.my_condition objectAtIndex:indexPath.row] intValue] == 0) { 
    NSLog(@"Store is closed"); 
} // ... 

代替,其假定值是一個NSNumber*或代表一個數字的NSString*

+0

這是NSNumber ..你的溶劑爲我工作,謝謝! – ghostrider

3

有沒有在這裏足夠的信息,以提供一個明確的答案,但我看到了幾種可能性。

  1. 該對象是一個帶尾隨空格的字符串。 "0"不等於"0 ",但它在記錄輸出中看起來相同。
  2. 該對象實際上是一個NSNumber,值爲0.它在記錄中看起來仍然相同,但它不會是一個平等的對象。嘗試檢查對象的類。
-1

嘗試翻轉的條件下,這樣的:

if ([[delegate.my_condition objectAtIndex:indexPath.row] isEqualToString:@"0"]) { 

NSString *_string = [delegate.my_condition objectAtIndex:indexPath.row]; 
if ([_string isEqualToString:@"0"]) { 

它可能具有呼籲像

+0

這完全沒有意義。事實上,如果所討論的對象真的是'NSNumber *'(如我所建議的那樣),那麼這將會崩潰。 –

0

字符串函數的煩惱是您的NSMutableArray返回一個字符串?這可能是因爲你正在返回一個NSNumber類型而不是一個字符串。在這種情況下,您可能希望將stringValue放在對象的末尾以將其轉換爲字符串。