加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

switch-statement – Swift 3,switch语句,case hasPrefix

发布时间:2020-12-14 02:25:21 所属栏目:百科 来源:网络整理
导读:在 Swift2中,您可以使用类似于以下代码的内容: switch productIdentifier { case hasSuffix("q"): return "Quarterly".localized case hasSuffix("m"): return "Monthly".localized default: return "Yearly".localized } 它会起作用.在Swift 3中,我能完成
在 Swift2中,您可以使用类似于以下代码的内容:
switch productIdentifier {
    case hasSuffix("q"):
        return "Quarterly".localized
    case hasSuffix("m"):
        return "Monthly".localized
    default:
        return "Yearly".localized
    }

它会起作用.在Swift 3中,我能完成上述工作的唯一方法是:

switch productIdentifier {
    case let x where x.hasSuffix("q"):
        return "Quarterly".localized
    case let x where x.hasSuffix("m"):
        return "Monthly".localized
    default:
        return "Yearly".localized
    }

这似乎失去了Swift2版本的清晰度 – 它让我觉得我错过了一些东西.以上是一个简单的版本.我很好奇是否有人有更好的处理方式?

我不知道这是否比在您的示例中使用值绑定更好,但您可以只使用下划线,
switch productIdentifier {
case _ where productIdentifier.hasSuffix("q"):
    return "Quarterly".localized
case _ where productIdentifier.hasSuffix("m"):
    return "Monthly".localized
default:
    return "Yearly".localized

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读