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
}
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
}
For functions that require a return, why don't returns in an if-else statement within a for loop suffice?
As array may be empty so compiler needs to assure all paths will return a valid value
func isEveryoneAdult(ages: [Int]) -> Bool {
for age in ages {
if age < 18 {
return false
}
}
return true
}
did Xcode 11 start ignoring missing 'return' statement errors?
Look at here: https://docs.swift.org/swift-book/LanguageGuide/Functions.html
Functions With an Implicit Return: If the entire body of the function
is a single expression, the function implicitly returns that
expression. For example, both functions below have the same behavior:
func greeting(for person: String) -> String {
"Hello, " + person + "!"
}
The entire definition of the greeting(for:) function is the greeting
message that it returns, which means it can use this shorter form.
Swift switch default + fallthrough: missing return in a function expected to return 'String'
It's not clear whether you have figured this out or not. The problem is not that you haven't got all bases covered in your switch statement. It is that not all code paths return a value. In the ApiError case, if the test fails, there is no return executed. If you put a return
after this test, the code will compile.
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 'UIImage'
This error occurs when not all paths of your code return a value. If you say your method returns a UIImage
, it must always do.
Let's take a closer look at the implementation of captureImage
:
func captureImage(_ sampleBuffer: CMSampleBuffer) -> UIImage {
.....
switch self.input.device.position {
case .front:
let resultImage = UIImage(cgImage: imageRef, scale: 1.0, orientation: UIImageOrientation.down)
return resultImage
case .back:
let resultImage = UIImage(cgImage: imageRef, scale: 1.0, orientation: UIImageOrientation.up)
return resultImage
default:
print("error")
}
}
We can see that if input.device.position
is .front
or .back
, the method returns a value. However, what if input.device.position
is neither of those values? The method will just print "error" and return nothing. That's not acceptable is it?
You might say, " I'm sure that input.device.position
can only be either front or back in this situation. It can't be anything else!" Well, the compiler isn't sure about that. It just sees that there are other possible values for input.device.position
.
In this case, I suggest that you just do fatalError()
when it's neither of those values. It will just crash your app. If you do this then the method doesn't need to return anything. The app is crashed after all.
Related Topics
iOS 8 Swift Audio Playback Execute Method on Completion
How to Compare Two Strings to Check If They Have Same Characters Swift 4
Swift - How to Set a Singleton to Nil
Linking Error When Building Parse in Xcode 7
How to Get All Text from a PDF in Swift
Updated Approach to Reauthenticate a User
Custom Cell with Uitableview Inside Uicollectionviewcell
How Does One Use Nsdateformatter's Islenient Option
"Ambiguous Use of 'Children'" When Trying to Use Nstreecontroller.Arrangedobjects in Swift 3.0
Parse.Com Pfquery Order by Time (Swift)
Why Does the Compiler Not See the Default Code in a Protocol
Get Element from Array of Dictionaries According to Key
Swiftui - Mapkit - Binding Mapkit and Show View on Annotation Callout Buttons
Is There a Neat Way to Represent a Fraction as an Attributed String
Ordering Firebase Posts Chronologically Swift