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 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.
How to fix missing return in function in swift but function does return
It's working for macOS?
Let's remove the noise of the #if
.
You can copy/paste, and comment the previous one.
We'll then keep only the code that's failing, the one inside the #if os(iOS)
and remove the code for #if os(macOS)
var image: Image {
if isStringLink(string: imageName) {
print("Loading Image from URL")
if var imageNameLink = URL(string: imageName) {
var imageData = try! Data(contentsOf: imageNameLink)
guard var UIImageResult = UIImage(data: imageData) else {
print("Failed to set image result\n\n Loading Image from Bundle")
Image(imageName) //**
return Image(imageName)
}
print("Returning Image")
Image(uiImage: UIImageResult) //HERE
} else {
print("Loading Image from Bundle")
Image(imageName) // HERE
}
} else {
print("Loading Image from Bundle")
Image(imageName) // HERE
}
}
You are not using return
in the last 3 times (marked with //HERE
), but you were returning something in case of macOS. Why should there be a difference?
Write return
before it. Also there is a Image(imageName)
before a return
(marked with //**
) which seems to be useless.
In the end:
var image: Image {
if isStringLink(string: imageName) {
print("Loading Image from URL")
if var imageNameLink = URL(string: imageName) {
var imageData = try! Data(contentsOf: imageNameLink)
guard var UIImageResult = UIImage(data: imageData) else {
print("Failed to set image result\n\n Loading Image from Bundle")
#if os(iOS)
return Image(imageName)
#endif
#if os(macOS)
return Image(imageName)
#endif
}
print("Returning Image")
#if os(iOS)
return Image(uiImage: UIImageResult)
#endif
#if os(macOS)
return Image(nsImage: UIImageResult)
#endif
} else {
print("Loading Image from Bundle")
#if os(iOS)
return Image(imageName)
#endif
#if os(macOS)
return Image(imageName)
#endif
}
} else {
print("Loading Image from Bundle")
#if os(iOS)
return Image(imageName)
#endif
#if os(macOS)
return Image(imageName)
#endif
}
}
Related Topics
How to Install Xcode on an External Hard Drive Along with the iPhone Simulator.App
Extend Existing Protocols to Implement Another Protocol with Default Implements
Keyboard Overlaying Action Sheet in iOS 13.1 on Cncontactviewcontroller
Converting Swift Array to Cfarray in Xcode 8 (Swift 3)
Does Swift Guarantee the Storage Order of Fields in Classes and Structs
How to Make Keyboard Dismiss When I Press Out of Searchbar on Swift
How to Display Realm Results in Swiftui List
Are the #If Debug Statements Really Needed for Previews in Swiftui to Remove It in a Release Build
What Are 'Get' and 'Set' in Swift
How to Improve Camera Quality in Arkit
Swift - Drawing Text with Drawinrect:Withattributes:
What Is the Practical Use of Nested Functions in Swift
Swiftier Swift for 'Add to Array, or Create If Not There...'
Get Build Date and Time in Swift
How Do People Deal with Iterating a Swift Struct Value-Type Property