ios - 错误:“预期返回 Int 的函数中缺少返回值”
问题描述
下面的第一个函数可以很好地检查 FM 值,但是当我在 else if 语句中为 AM 值添加一些验证时,我收到错误消息: Missing return in a function expected to return Int
. 花括号的放置似乎不是问题。
static var minAMFFrequency: Double = 520.0
static var maxAMFFrequency: Double = 1610.0
static var minFMFFrequency: Double = 88.3
static var maxFMFFrequency: Double = 107.9
func isBandFM() -> Int {
if frequency >= RadioStation.minFMFFrequency && frequency <= RadioStation.maxFMFFrequency {
return 1 //FM
} else {
return 0 //AM
}
}
修改后的函数有错误:
func isBandFM() -> Int {
if frequency >= RadioStation.minFMFFrequency && frequency <= RadioStation.maxFMFFrequency {
return 1 //FM
} else if frequency >= RadioStation.minAMFFrequency && frequency <= RadioStation.maxAMFFrequency{
return 0 //AM
}
}
解决方案
您还需要考虑不属于这两种情况的情况。它希望您提供默认返回值。
在第一种情况下,您返回了默认值 0。
第二种情况,如果你frequency
既不在第一个范围(由第一个if
条件指定)也不在第二个范围(由第二个if
条件指定),则需要指定一个默认返回值。
func isBandFM() -> Int {
if frequency >= RadioStation.minFMFFrequency && frequency <= RadioStation.maxFMFFrequency {
return 1 //FM
} else if frequency >= RadioStation.minAMFFrequency && frequency <= RadioStation.maxAMFFrequency{
return 0 //AM
}
return 0 // or whatever value you want to return if frequency is not within FM range or AM range
}
推荐阅读
- haskell - 如何指示黑线鳕链接到 hackage 包的 hackage 文档?
- macos - 如何连接到 macOS 并从 docker 映像执行脚本
- python - 代码产生 'AttributeError' 而不是正确引发 ValueError
- node.js - 文件上传并通过 POST 请求向 API 发送信息
- shiny - 初始化应用程序时,条件面板消息闪烁然后消失
- python - 气流在initdb后无法进入/admin页面
- reporting-services - SSRS 如何动态进行范围查找?使用参数
- mysql - 如何在 MySQL 中的“案例”中命名表的多个字段?
- javascript - 一个事件监听器,用于所有具有相同父级的元素,即使您将一些元素放置在代表多个节点的变量中
- python - Python 3:_tkinter.TclError:无效的命令名称“.!scrollbar”