1

好吧,这是我的代码:

    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 但我没有看到 if 子句而是 else 子句。

这是为什么?

myCondition is NSMutableArray

这就是我将项目添加到我的 NSMutableArray 的方式:

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

4 回答 4

3

这里没有足够的信息来提供明确的答案,但我看到了一些可能性。

  1. 该对象是一个带有尾随空格的字符串。"0"不等于"0 ",但在日志输出中看起来是一样的。
  2. 该对象实际上是一个NSNumber,值为 0。它在日志记录中看起来仍然相同,但它不是一个相等的对象。尝试检查对象的类。
于 2012-08-29T19:43:52.863 回答
0

您确定其中的值delegate.my_condition是字符串吗?那些看起来可能是NSNumber*s。当然, anNSNumber*永远不会等于 an NSString*

您可能想尝试类似的东西

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

相反,它假定值是表示数字的 anNSNumber*或 an 。NSString*

于 2012-08-29T19:44:01.423 回答
0

您的 NSMutableArray 是否返回字符串?这可能是因为您返回的是 NSNumber 类型而不是字符串。在这种情况下,您可能希望将 stringValue 放在对象的末尾以将其转换为字符串。

于 2012-08-29T19:47:28.717 回答
-1

尝试翻转条件,如下所示:

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

或者

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

在这样的字符串上调用函数可能会遇到问题

于 2012-08-29T19:45:25.387 回答