The first function below works fine to check for FM values, but when I added some validation in an else if statement for the AM values, I am getting error message saying: Missing return in a function expected to return Int. It doesn't seem to be an issue with the placement of the curly braces.
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
}
}
Modified function with error:
func isBandFM() -> Int {
if frequency >= RadioStation.minFMFFrequency && frequency <= RadioStation.maxFMFFrequency {
return 1 //FM
} else if frequency >= RadioStation.minAMFFrequency && frequency <= RadioStation.maxAMFFrequency{
return 0 //AM
}
}
isBandFM()return iffrequencyis neither in the AM or FM band? The compiler can't guess for you.return 1 //FM, that's a code smell. You knew the1is unclear, so you felt the need to explain it with a comment. Instead, you should consider replacingInt(which is a very "weak" type in this context), with something "stronger" and more self-descriptive, likeenum. I suggest you check out youtube.com/watch?v=_S6UOrwS-Tg