【问题标题】:Objective C integer array [duplicate]Objective C整数数组[重复]
【发布时间】:2018-06-15 11:50:54
【问题描述】:

我有一个名为 binaryArray 的数组。

NSMutableArray *binaryArray = [[NSMutableArray alloc] init];初始化

我使用 [binaryArray addObject:[NSNumber numberWithInt:1]]; 将 0 或 1 添加到数组中

然后我在 tableView 中引用这个数组

id resultBinary = [binaryArray objectAtIndex:indexPath.item];

    if (resultBinary == 0) {
        ...
    } else {
        ...
    }

但是,即使resultBinary0,它也永远不会命中

【问题讨论】:

    标签: ios objective-c arrays object integer


    【解决方案1】:

    因为 NSNumber 是一个有助于将数字类型存储为对象的类。所以和 1 比较时,实际上是在和数值类型比较,而不是和 NSNumber 比较。

    因此将其更改为 NSNumber 字面量是可行的。

    id resultBinary = [binaryArray objectAtIndex:indexPath.item];
    
        if ([resultBinary isEqual:@0]) {
            [cell.starImage setImage:[UIImage imageNamed:@"Results_star_empty"]];
        } else {
            [cell.starImage setImage:[UIImage imageNamed:@"Results_star_filled"]];
        }
    

    Objective-C Literals

    【讨论】:

      【解决方案2】:

      试试这个

      if ([resultBinary integerValue] == 0) {
          ...
      } else {
          ...
      }
      

      【讨论】:

        猜你喜欢
        • 2011-06-02
        • 2011-02-17
        • 2011-10-21
        • 2011-08-05
        • 2013-05-31
        • 1970-01-01
        • 2013-05-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多