在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版本的清晰性--这让我觉得我错过了什么。当然,上面是一个简单的版本。我很好奇有没有人能更好地处理这件事?
发布于 2016-11-08 05:16:30
我不知道这是否比在示例中使用值绑定更好,但是您可以只使用下划线,
switch productIdentifier {
case _ where productIdentifier.hasSuffix("q"):
return "Quarterly".localized
case _ where productIdentifier.hasSuffix("m"):
return "Monthly".localized
default:
return "Yearly".localized发布于 2016-10-23 03:43:00
您似乎只检查productIdentifier的最后一个字符。你可以这样做:
switch productIdentifier.last {
case "q"?:
return "Quarterly".localized
case "m"?:
return "Monthly".localized
default:
return "Yearly".localized
}https://stackoverflow.com/questions/40199009
复制相似问题