5

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
        }
    }
}
1
  • 1
    Not familiar with swift, but to hazard a guess, I think it is complaining that not all code paths return a value. What would happen if the 'find' method returns false? Your method does not return anything in that case! Commented Jul 11, 2014 at 3:11

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep you're exactly correct! You can not have a function where there is ever a possibility of not returning a value, the SWIFT compiler will balk at any code that does not return in all circumstances.
2

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
}

Comments

0

You could use a switch statement. Here is an example I have used:

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let button = DropDownButton()
    switch button {
    case userButton:
        return userButton.dropView.dropDownOptions.count
    case categoryButton:
        return categoryButton.dropView.dropDownOptions.count
    default:
        return 0
    }

Thought it might help somebody, Happy coding

Comments

0
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let button = DropDownButton()
    switch button {
    case userButton:
        return userButton.dropView.dropDownOptions.count
    case categoryButton:
        return categoryButton.dropView.dropDownOptions.count
    default:
        return 0
    }

1 Comment

Hey, it helps a lot if you use the code formatting in your replies. Just three back-ticks on a single line above and below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.