I am creating a tic tac toe game and I came upon this error that I don't really understand: "Missing return in a function expected to return '(location:String, pattern:String)'". It is drawing the error on this block of code that should return "algorResults", which should give a location and pattern (both Strings) as was initialized on the first line. What's going wrong? Please thoroughly explain suggestions, thanks! My code:
//Function that implements part of AI that sees where a good place to play is:
func rowcheck(value:Int)->(location:String, pattern:String)? {
var goodFinds = ["011","101","110"]
var findFuncs = [checktop, checkbottom, checkmidAcross, checkleft, checkmidDown, checkright, checkLRdiag, checkRLdiag]
for algor in findFuncs {
var algorResults = algor(value)
if find(goodFinds,algorResults.pattern) {
return algorResults
}
}
}
Your code only returns a value if find(goodFinds,algorResults.pattern)
is satisfied, but there is no return value otherwise.
What you can do is to specify a default return value and do something like this (pseudo-code only):
variable result = defaultvalue
if find(goodFinds,algorResults.pattern) {
result = algorResults
}
return result
I should mention that I am not familiar with Swift, but this logic would ensure that you always return something, so that your method fulfills the contract.
Since your return type is an optional, you could do this:
func rowcheck(value:Int)->(location:String, pattern:String)? {
var goodFinds = ["011","101","110"]
var findFuncs = [checktop, checkbottom, checkmidAcross, checkleft, checkmidDown, checkright, checkLRdiag, checkRLdiag]
for algor in findFuncs {
var algorResults = algor(value)
if find(goodFinds,algorResults.pattern) {
return algorResults
}
}
return nil
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With