2012-08-29 20 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放在对象的末尾以将其转换为字符串。