【问题标题】:What is this Swift ternary operator? A == B ? C : D这个 Swift 三元运算符是什么?甲==乙? C : D
【发布时间】:2017-05-16 14:11:52
【问题描述】:

我继承了一个在此行崩溃的 iOS 应用程序(意外为零)。您对这里发生的事情的最佳解释是什么?

indexPath.row.number == selectedItem ? cell.deselectStyle() : cell.dedeselectStyle()

cell.deselectStyle() 和 cell.dedeselectStyle() 函数不返回任何内容。我似乎找不到任何关于这里发生了什么的信息。 selectedItem 是 NSNumber!。

【问题讨论】:

  • selectedItem 为零,这就是它崩溃的原因
  • 我明白了,我只是想知道? 之后的所有内容有什么意义。
  • if (indexPath.row.number == selectedItem){cell.deselectStyle()} else{cell.dedeselectStyle()} 他们返回的东西没关系。这不是您似乎误解的if (selectedItem){indexPath.row.number = cell.deselectStyle()} else{indexPath.row.number = cell.dedeselectStyle()}
  • 如果条件为真,则调用这个cell.deselectStyle()。如果为 false,则调用此 cell.dedeselectStyle()。
  • 哦,好的,我现在看到了。

标签: swift legacy-code


【解决方案1】:

NSNumber 可能是nil,如果您尝试访问它会导致崩溃。添加guard 以检查它不是nil。

guard let s = selectedItem?.intValue else {
    cell.dedeselectStyle
    return
}

indexPath.row == s ? cell.deselectStyle() : cell.dedeselectStyle()

我假设如果 NSNumber 为 nil,则假设未选择单元格是安全的,但是您应该真正检查代码中的逻辑以确定。

【讨论】:

    【解决方案2】:

    这是一个条件语句。把它想象成if 声明:

    if indexPath.row.number == selectedItem {
        cell.deselectStyle()
    } else {
        cell.dedeselectStyle()
    }
    

    如果条件为真,? 和: 之间的代码将被执行。如果没有,将调用: 之后的代码。您应该知道? 与Optionals 无关。

    在您的情况下,selectedItem 似乎是 nil。因此,如果selectedItem 不是nil,您只需要执行代码,您可以使用if let 语句:

    if let selectedItem = selectedItem {
        indexPath.row.number == selectedItem ? cell.deselectStyle() : cell.dedeselectStyle()
    }
    

    或者,如果是nil,您可以插入一个默认值,而不是selectedItem:

    indexPath.row.number == (selectedItem ?? false) ? cell.deselectStyle() : cell.dedeselectStyle()
    

    如果selectedItem 是nil,上述代码将使用selectedItem 或false 的值。您可以省略括号,我只是将它们放在那里以便更好地可视化。

    我希望这会有所帮助:)

    【讨论】:

      【解决方案3】:

      可能崩溃的原因是selectedItem是nil;我认为您误解了 optionals 的 ? 和实现三元运算符的 ? 之间的区别。

      三元运算符与 - 隐式检查值是否为 nil 无关,在进行三元运算符比较之前,您可能需要实现 guard 语句或 if let(可选绑定)。

      应该类似于:

      if let selectedItem = selectedItem {
          indexPath.row.number == selectedItem ? cell.deselectStyle() : cell.dedeselectStyle()
      } else {
          print("selectedItem is nil!")
      }
      

      或者,如果需要提前退货:

      guard let selectedItem = selectedItem else {
          print("selectedItem is nil!")
          return
      }
      
      indexPath.row.number == selectedItem ? cell.deselectStyle() : cell.dedeselectStyle()
      

      为了让你更清楚,检查以下代码sn-p:

      let nilValue: String! = nil
      
      if nilValue == "test" { }
      // fatal error: unexpectedly found nil while unwrapping an Optional value
      
      // OR
      let isValueTest: Bool = (nilValue == "test") ? true : false
      // fatal error: unexpectedly found nil while unwrapping an Optional value
      

      这就是为什么你应该在访问它之前解包它。

      【讨论】:

        【解决方案4】:

        为了更好地理解它,请在您的 Playground 中玩耍

        func f0() -> Int { print(0); return 0 }
        func f1() -> Int { print(1); return 1 }
        func fa() -> String { print("a"); return "a" }
        
        [true, false, true, true].forEach {
            $0 ? f0() : f1() // warning: expression of type 'Int' is unused
        }
        
        [true, false, true, true].forEach {
           _ = $0 ? f0() : f1() // OK
        }
        
        [true, false, true, true].forEach {
            $0 ? f0() : fa() // error: result values in '? :' expression have mismatching types 'Int' and 'String'
        }
        
        [true, false, true, true].forEach {
            _ = $0 ? ( 1 == f0()) : ( "A" == fa()) // OK
        }
        

        【讨论】:

          猜你喜欢
          • 2020-11-05
          • 1970-01-01
          • 2015-12-09
          • 1970-01-01
          • 2014-12-11
          • 1970-01-01
          • 2020-04-12
          • 2017-12-09
          • 2020-10-16
          相关资源
          最近更新 更多