微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐