如何编写使用这些异步模式的代码?斯威夫特4

时间:2018-12-27 16:01:36

标签: swift swift4

在解析封闭中返回布尔值时,我确实遇到了问题,因为我遇到了以下错误:Unexpected non-void return value in void function

我保证我已经做过研究并且尝试了一些东西,但这不起作用。我15岁,自学代码,所以请放纵。

我想在shouldPerformSegue()函数中调用此函数并存储结果,以便在代码中进一步使用它。

// function that checks if username is already in database
func isUsernameAlreadyTaken(givenUsername: String) -> Bool {    
  let query = PFUser.query()    
  query?.findObjectsInBackground(block: { (users, error) in        
    if error != nil {
      print(error?.localizedDescription)
    } else if let users = users {
      for object in users {
        if let user = object as? PFUser {
          if let username = user.username {
            if username == givenUsername {
              return true // here is the error
            }
          }
        }
      }
    }
  })
  return false // here is the error
}

// we use this function to "deactivate" the segue if there is something wrong with entry
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
  if identifier == "toSignUp2VCsegue" {
    /// ... just doing several conditions here
    if isUsernameAlreadyTaken(givenUsername: pseudoTxtField.text!) == true {
      return false
    }
  }
}

请帮助我,我已经努力了将近2天来尝试一些东西...

0 个答案:

没有答案