【问题标题】:Multiple if let and OR condition in swiftswift中的多个if let和OR条件
【发布时间】:2020-01-24 22:42:02
【问题描述】:

我有三个可选值。这些值是从 JSON 中读取的。首先我想用 if let 保护这些值,然后我想检查这些值本身。

我想检查其中一个值是否存在,然后做一些事情。

我知道一个语句中的多个 if let 是 && 条件。我怎么做 ||代码更少的条件?

JSON:

{value1="", value2="", value3="Some value"}

struct Profile: Decodable
    let value1: String?
    let value3: String?
    let value3: String?
}

以下是伪代码:

if let value1 = profile.value1 || let value2 = profile.value2 || let value3 = profile.value3 {
    // One of these value is present
    if value1 != "null" {
        // do something
    } else if value2 != "null" {
        // do something
    } else if value3 != "null" {
        // do something
    } else {
        // No value present
    }
} else {
    // None of these values is present
}

【问题讨论】:

  • 这能回答你的问题吗? if let with OR condition
  • 不完全。看来我最终会得到一个嵌套的多重和复杂的 if else。我想知道是否有更简单的解决方案。
  • 如何在外部then 分支中使用value2value3?你需要它们吗?如果没有,那两个简单的!= nil 就足够了。
  • 我会检查并使用任何值(如果存在)。
  • @P2000 如果您检查编辑历史记录,问题非常模糊,并且接受的答案不仅没有真正回答问题的要求(该答案中没有 OR 逻辑),而且它也很基本,所以我会说 OP 并没有很好地询问,即使在编辑之后也是如此。

标签: swift


【解决方案1】:

if let 语句不能使用 OR 条件。或者,您可以尝试以下选项。

首先,

      if let _ = value1 {
            //One is present
        } else if let  _ = value2 {
            //One is present
        } else if let  _ = value3 {
            //One is present
        }

第二,

if let value = value1 ?? value2 ?? value3 {
    print(value)
}

【讨论】:

  • 非常好 if let value = value1 ?? value2 ?? value3 确实有效:值获取第一个非零。但为什么呢?
  • @P2000 这就是所谓的Nil-Coalescing Operator
【解决方案2】:
if let value1 = profile.value1, value1 != “” {}
else if value2 = profile.value2, value2 != “” {}
else if value3 = profile.value3, value3 != “” {}

这里, 的行为类似于 &&

注意: 对于 Swift 2,将 , 替换为 where

【讨论】:

  • @Raymond 祝你好运
【解决方案3】:

如果您只想检查至少一个不是nil,您可以与nil 进行比较。

if value1 != nil || value2 != nil || value3 != nil {
    // One of these values is present
} else {
    // None of these values is present
}

如果要使用不是nil 的值,则取决于变量的类型。如果它们都是同一类型,您可以像这样使用nil coalescing

if let value = value1 ?? value2 ?? value3 {
    // One of these values is present
} else {
    // None of these values is present
}

这会将value 绑定到那些不是nil 的变量中的第一个变量,但只有当它们是相同类型或它们符合共享协议时它才会起作用(您可能需要强制转换它们)在这种情况下)。现在您将能够使用value 作为共享类/协议。

如果它们是不同的类型,您可以将值强制转换为 Any,但这并不是很有用,因为您必须检查其类型并采取相应措施,相反我会为每个变量使用 if let

【讨论】:

    【解决方案4】:

    不要使用if-let,而是尝试检查变量中的nil

    if value1 != nil || value2 != nil || value3 != nil {
        // One of these value is present
    } else {
        // None of these values is present
    }
    

    【讨论】:

      【解决方案5】:

      一种解决方案是使用带有由变量组成的元组的switch statmenent

      switch (value1, value2, value3) {
      case let (.some(value1), _, _):
          ...
      case let (_ , .some(value2), _):
          ...
      case let (_ ,_ , .some(value3)):
          ...
      default: 
      print("all nil")    
      }
      

      .some 用于解包变量。

      【讨论】:

      • 令人兴奋的编码技巧。
      猜你喜欢
      • 2021-04-02
      • 1970-01-01
      • 2017-04-02
      • 2015-03-25
      • 2017-01-27
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多