Swift 2: expression pattern of type #39;Bool#39; cannot match values of type #39;Int#39;(Swift 2:“Bool类型的表达式模式不能匹配“Int类型的值)
问题描述
我正在做这个问题集FizzBuzz",我的 switch 语句给了我一些问题,这是我的代码:
I'm doing this problem set "FizzBuzz", and my switch statement is giving me some problems, here's my code:
func fizzBuzz(n: Int) -> String {
switch n {
case n % 3 == 0: print("Fizz")
case n % 5 == 0: print("Buzz")
case n % 15 == 0:print("FizzBuzz")
}
return "(n)"
}
如果你能给我提供指针/提示,而不是给我正确的代码,那就太好了:D我更愿意自己解决它,但一些提示可以让我摆脱这个困境.
If you could provide me with pointers / hints, instead of giving me the correct code, that would be swell :D I'd prefer solving it myself, but a few hints could get me out of this hole.
推荐答案
您可以使用 case let where
并在单独检查它们之前检查两者是否匹配:
You can use case let where
and check if both match before checking them individually:
func fizzBuzz(n: Int) -> String {
let result: String
switch n {
case let n where n.isMultiple(of: 3) && n.isMultiple(of: 5):
result = "FizzBuzz"
case let n where n.isMultiple(of: 3):
result = "Fizz"
case let n where n.isMultiple(of: 5):
result = "Buzz"
default:
result = "none"
}
print("n:", n, "result:", result)
return result
}
这篇关于Swift 2:“Bool"类型的表达式模式不能匹配“Int"类型的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swift 2:“Bool"类型的表达式模式不能匹配“Int"类型的值


基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01