Swift Error: Missing Return in a Function Expected to Return 'String'

Error: 'Missing return in a function expected to return Int'

You also need to consider cases that do not fall in the 2 conditions. It expects you to provide a default return value.

In the first case, you had a default value of 0 being returned.

In the second case, if your frequency is neither in the first range (specified by the first if condition) nor in the second range (specified by the second if condition), you need to specify a default return value.

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
}

Missing return in a function expected to return 'Double?'

Your function can be shortened a hell of a lot by taking advantage of some of the tools that’s Swift gives you. Here, take a look, this does the same thing...

func priceCheck(name: String) -> Double? {
guard let stockAmount = stock[name],
stockAmount > 0,
let price = prices[name] else {
return nil
}

return price
}

Missing return in global function expected to return 'String' message in a function

Your responseTo(String) -> String function must return a String. You must consider this case, if the parameter (question) doesn't start with "hello", the function doesn't have any String to return.

let result: String = responseTo("asd") // error

As stated in the comments, there are several ways to solve this.
If your function must return a string, then consider returning a default value at the end. The return value can be an empty string (but whatever your default value is, make sure to handle it properly).

func responseTo(question: String) -> String {
let lowercasedQuestion = question.lowercased()
if lowercasedQuestion.hasPrefix("hello") {
//
} else {
return "" // this will be the default return value
}
}

or

func responseTo(question: String) -> String {
let lowercasedQuestion = question.lowercased()
if lowercasedQuestion.hasPrefix("hello") {
//
}
return "" // this will also be the default return value
}

Another way is to return an Optional String (String?). The reason why return nil doesn't work for responseTo(String) -> String is because it must return a string. To be able to return a nil, you will have to change the declaration of your function to responseTo(String) -> String?.

func responseTo(question: String) -> String? { // the "?" after String notifies the compiler that this function can return a String or a nil
let lowercasedQuestion = question.lowercased()
if lowercasedQuestion.hasPrefix("hello") {
//
}
return nil
}

you can read more about function here and optional here

missing return in a function expected to return 'Double' | What to do?

The issue is that your if statement is missing an else block, the return value is not defined for the case when self == 0. You can simply change the else if branch to else, because you also want to return self for 0.

var absoluteValue: Double {
if self < 0 {
return self * -1
} else {
return self
}
}

You can also write this as a oneliner using the ternary operator:

var absoluteValue: Double {
return self < 0 ? self * -1 : self
}


Related Topics



Leave a reply



Submit