swift switch语句在可选的布尔元组上

 dmcm0004 发布于 2023-01-07 12:19

我无法弄清楚如何在交换机内的元组内使用选项.下面..Some(let ...)...语法作为非元组工作,但在元组内我得到预期的分隔符东西:(

var dict = Dictionary()
dict["a"] = true

switch (dict["a"],dict["b") {
case (.Some(let a) where !a, .Some(let b) where b):
  println("false/nil, true")
case (.Some(let a) where a, .Some(let b) where !b):
  println("true, false/nil")

我想避免做以下事情

    if let a = self.beaconList["a"] {
        if let b = self.beaconList["b"] {
            // a, b
        } else {
            // a, !b
        }
    } else {
        if let b = self.beaconList["b"] {
            // !a, b
        } else {
            // !a, !b
        }
    }

Brett.. 17

有很多方法可以做到这一点,但是为了修复你试图按字面意思做的语法,你需要显式地取消选择Optional,或者通过匹配.Some(false),或者解包它!(我认为这有点奇怪,你会看到)

var dict = Dictionary()
dict["a"] = true
dict["c"] = false

func matchOneOrTheOtherWithOptionals(a: Bool?, b: Bool?) -> String {
    switch (a, b) {
    case (.Some(true), let b) where b == .None || !b!: // gross
        return "a was true, but b was None or false"
    case (let a, .Some(true)) where a == .None || a == .Some(false):
        return "a was None or false and b was true"
    default:
        return "They both had a value, or they were both missing a value"
    }
}

matchOneOrTheOtherWithOptionals(true, .None) // "a was true, but b was None or false"
matchOneOrTheOtherWithOptionals(true, false) // "a was true, but b was None or false"
matchOneOrTheOtherWithOptionals(.None, true) // "a was None or false and b was true"
matchOneOrTheOtherWithOptionals(false, true) // "a was None or false and b was true"

matchOneOrTheOtherWithOptionals(false, false) // "They both had a value, or they were both missing a value"
matchOneOrTheOtherWithOptionals(true, true) // "They both had a value, or they were both missing a value"
matchOneOrTheOtherWithOptionals(.None, .None) // "They both had a value, or they were both missing a value"

您还可以尝试以下方法:

func noneToFalse(bool: Bool?) -> Bool {
    if let b = bool {
        return b
    } else {
        return false
    }
}

func matchOneOrTheOther(a: Bool, b: Bool) -> String {
    switch (a, b) {
    case (true, false):
        return "a is true, b was false or None"
    case (false, true):
        return "a was false/None, b was true"
    default:
        return "both were true, or both were false/None"
    }
}

matchOneOrTheOther(noneToFalse(dict["a"]), noneToFalse(dict["b"]))

这是我在写这个答案时使用的游乐场的要点:https://gist.github.com/bgrace/b8928792760159ca58a1

1 个回答
  • 有很多方法可以做到这一点,但是为了修复你试图按字面意思做的语法,你需要显式地取消选择Optional,或者通过匹配.Some(false),或者解包它!(我认为这有点奇怪,你会看到)

    var dict = Dictionary<String,Bool>()
    dict["a"] = true
    dict["c"] = false
    
    func matchOneOrTheOtherWithOptionals(a: Bool?, b: Bool?) -> String {
        switch (a, b) {
        case (.Some(true), let b) where b == .None || !b!: // gross
            return "a was true, but b was None or false"
        case (let a, .Some(true)) where a == .None || a == .Some(false):
            return "a was None or false and b was true"
        default:
            return "They both had a value, or they were both missing a value"
        }
    }
    
    matchOneOrTheOtherWithOptionals(true, .None) // "a was true, but b was None or false"
    matchOneOrTheOtherWithOptionals(true, false) // "a was true, but b was None or false"
    matchOneOrTheOtherWithOptionals(.None, true) // "a was None or false and b was true"
    matchOneOrTheOtherWithOptionals(false, true) // "a was None or false and b was true"
    
    matchOneOrTheOtherWithOptionals(false, false) // "They both had a value, or they were both missing a value"
    matchOneOrTheOtherWithOptionals(true, true) // "They both had a value, or they were both missing a value"
    matchOneOrTheOtherWithOptionals(.None, .None) // "They both had a value, or they were both missing a value"
    

    您还可以尝试以下方法:

    func noneToFalse(bool: Bool?) -> Bool {
        if let b = bool {
            return b
        } else {
            return false
        }
    }
    
    func matchOneOrTheOther(a: Bool, b: Bool) -> String {
        switch (a, b) {
        case (true, false):
            return "a is true, b was false or None"
        case (false, true):
            return "a was false/None, b was true"
        default:
            return "both were true, or both were false/None"
        }
    }
    
    matchOneOrTheOther(noneToFalse(dict["a"]), noneToFalse(dict["b"]))
    

    这是我在写这个答案时使用的游乐场的要点:https://gist.github.com/bgrace/b8928792760159ca58a1

    2023-01-07 12:21 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有