小编Ale*_*exK的帖子

Swift 3,switch语句,case hasPrefix

在Swift2中,您可以使用类似于以下代码的内容:

    switch productIdentifier {
    case hasSuffix("q"):
        return "Quarterly".localized
    case hasSuffix("m"):
        return "Monthly".localized
    default:
        return "Yearly".localized
    }
Run Code Online (Sandbox Code Playgroud)

它会起作用.在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
    }
Run Code Online (Sandbox Code Playgroud)

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

switch-statement swift3

5
推荐指数
1
解决办法
2441
查看次数

标签 统计

swift3 ×1

switch-statement ×1