其他功能获得价值但不是其他[重复]

Posted

技术标签:

【中文标题】其他功能获得价值但不是其他[重复]【英文标题】:Other Function get value but not the other [duplicate] 【发布时间】:2019-11-14 03:18:31 【问题描述】:

在我加载用户的函数中,我可以检索一个值。但是,当我想将它分配给函数外部的变量时,它什么都没有,如登录函数所示。

加载用户函数

func loadUser(userid: String) -> User 
   //print(userid)
   let userid = "56ldZFJiv0dpfaABzo78"
   var user = User()
   let docRef = db.collection("users").document(userid)
   docRef.getDocument  (document, error) in
       if let document = document 
           let first = document.data()!["first"] as! String
           let last = document.data()!["last"] as! String
           let position = document.data()!["position"] as! String
           let company = document.data()!["company"] as! String
           let email = document.data()!["email"] as! String
           let address = document.data()!["address"] as! String
           let userID = document.data()!["userID"] as! String

           //Initalize user
          user =  User(userID: userID,
                                    firstName: first,
                                    lastName: last,
                                    company: company,
                                    address: address,
                                    position: position,
                                    email: email)

           print(user.position)
        else 
           print("Document does not exist")
       
   
   return user

登录功能

//MARK: LOGIN
func login() 
    Auth.auth().signIn(withEmail: emailField.text!, password: passwordField.text!)  (user, error) in
        if error == nil
            //self.performSegue(withIdentifier: "loginToAdmin", sender: self)

            //Load user
            let loggedOnUser = self.loadUser(userid: Auth.auth().currentUser!.uid)
            print(loggedOnUser.userID)
//                let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
//                let chatViewController = storyBoard.instantiateViewController(withIdentifier: "chatVC") as! UINavigationController
//                self.present(chatViewController, animated: true, completion: nil)
        
        else 
            DispatchQueue.main.async
                //Display Alert Message if login failed
                let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
                let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                alertController.addAction(defaultAction)
                self.present(alertController, animated: true, completion: nil)
            
        
    

对于第一个函数,我得到一个位置值,如打印语句中所述。 对于第二个函数,我的变量“loggedOnUser”为空。

【问题讨论】:

getDocument 是异步的。您不能返回它获取的用户。 programmingios.net/returning-a-value-from-asynchronous-code 注意你使用let userid = "56ldZFJiv0dpfaABzo78",它隐藏了函数参数userid: String 重复Returning data from async call in Swift function 【参考方案1】:

你需要一个完成,因为loadUser 是异步的

func loadUser(userid: String,completion:@escaping(User?) ->()) 
       //print(userid)
       let userid = "56ldZFJiv0dpfaABzo78"
       var user = User()
       let docRef = db.collection("users").document(userid)
       docRef.getDocument  (document, error) in
           if let document = document 
               let first = document.data()!["first"] as! String
               let last = document.data()!["last"] as! String
               let position = document.data()!["position"] as! String
               let company = document.data()!["company"] as! String
               let email = document.data()!["email"] as! String
               let address = document.data()!["address"] as! String
               let userID = document.data()!["userID"] as! String

               //Initalize user
              user =  User(userID: userID,
                                        firstName: first,
                                        lastName: last,
                                        company: company,
                                        address: address,
                                        position: position,
                                        email: email)

               print(user.position)
               completion(user)
            else 
               print("Document does not exist")
               completion(nil)
           
        
   

打电话

 self.loadUser(userid: Auth.auth().currentUser!.uid)  res in
     if let user = res 
        print(user)
     

【讨论】:

以上是关于其他功能获得价值但不是其他[重复]的主要内容,如果未能解决你的问题,请参考以下文章

为其他控制器的功能添加价值

有没有办法在不使用循环的情况下获得价值?

如何从faker获得独特的价值?

如何在调试 PL SQL 脚本时获得 clob 的全部价值

使用ajax无法在我的控制器中获得价值? [重复]

一旦我从 Promise 中获得价值,就渲染 li 价值