Swift error: 'missing return in function'?

前端 未结 4 1724
无人共我
无人共我 2020-12-11 18:35

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:St

相关标签:
4条回答
  • 2020-12-11 19:09

    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
    }
    
    0 讨论(0)
  • 2020-12-11 19:10
    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
        }
    
    0 讨论(0)
  • 2020-12-11 19:11

    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

    0 讨论(0)
  • 2020-12-11 19:14

    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.

    0 讨论(0)
提交回复
热议问题