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
How to Pause and Resume Nstimer.Scheduledtimerwithtimeinterval in Swift
Implicitly Import Specific Swift Module
Xcode Beta 6 "Type of Expression Is Ambiguous Without More Context" Navigationlink
Arkit - Viewport Size VS Real Screen Resolution
Is This a Good Way to Display Asynchronous Data
Single-Element Parethesized Expressions/Tuples VS Common Use of Parentheses
How to Retrieve a Random Object from Firebase Using a Sequential Id
What's the Difference Between If Nil != Optional … and If Let _ = Optional …
How to Create Nsradiobutton Group in Xcode 7 Osx
Store a Closure as a Variable in Swift
How to Change the Associated Values of a Enum
Ios8 Custom Keyboard - Copy & Paste to Uipasteboard
In Swift, Can You Split a String by Another String, Not Just a Character
Swiftui - Wait Until Firestore Getdocuments() Is Finished Before Moving On
How Replace Position++ Code to Make It Swift 3 Compatible
Expandable Sections Uitableview Indexpath Swift