简体   繁体   中英

Escaping closure captures non-escaping parameter 'function' Xcode says

I'm trying to perform a segue after two API requests are complete, and I have all the data I need. I'm trying do this because the requests take a bit long. However, when I tried to do something like this post , I got these errors:

1. Passing a non-escaping function parameter 'anotherFunc' to a call to a non-escaping function parameter can allow re-entrant modification of a variable
2. Escaping closure captures non-escaping parameter 'anotherFunc'
3. Escaping closure captures non-escaping parameter 'function'

I added comments where the errors show up. Here's my code:

func getUsernameRequest(function: () -> Void) {
            // send an API request to get the userinfo
            let url = "\(K.API.baseAPIEndpoint)/user"
            let headers: HTTPHeaders = [...]
            AF.request(url, headers: headers).responseDecodable(of: UserModel.self) { response in // this is where the #3 error shows up
                if let value = response.value {
                    self.username = value.username
                    // and do other things
                    function()
                } else {
                    offlineBanner()
                }
            }
        }
        
        func getMessagesRequest(function: (()->Void)->Void, anotherFunc:()->Void) {
            let postDetailUrl = "\(K.API.baseAPIEndpoint)/posts/\(postArray[indexPath.row].id)"
            let headers: HTTPHeaders = [...]
            // send an API request to get all the associated messages
            AF.request(postDetailUrl, headers: headers).responseDecodable(of: PostDetailModel.self) { response in // this is the #2 error shows up
               if let value = response.value {
                   self.messages = value.messages
                   function(anotherFunc) // this is where the #1 error shows up
               } else {
                   offlineBanner()
               }
            }
        }
        
        func performSegueToChat() -> Void {
            self.performSegue(withIdentifier: K.Segue.GotoChat, sender: self)
        }
        
        getMessagesRequest(function: getUsernameRequest, anotherFunc: performSegueToChat)

Thank you in advance.

I did this the following code, and it works as I expected! (But they aren't too clean)

        func getUsernameRequest(function: @escaping() -> Void) {
            // send an API request to get the username
            let url = "\(K.API.baseAPIEndpoint)/user"
            AF.request(url, headers: headers).responseDecodable(of: GetUsernameModel.self) { response in
                    if let value = response.value {
                        self.username = value.username
                        function()
                    } else {
                        offlineBanner()
                    }
                }
        }
        
        func getMessagesRequest(function: @escaping(@escaping()->Void)->Void, anotherFunc: @escaping()->Void) {
            let postDetailUrl = "\(K.API.baseAPIEndpoint)/posts/\(postArray[indexPath.row].id)"
            // send an API request to get all the associated messages and put them into messages array
            AF.request(postDetailUrl, headers: headers).responseDecodable(of: PostDetailModel.self) { response in
                if let value = response.value {
                    self.messages = value.messages
                    function(anotherFunc)
                } else {
                    offlineBanner()
                }
            }
        }
        
        func performSegueToChat() -> Void {
            self.performSegue(withIdentifier: K.Segue.GotoChat, sender: self)
        }
        
        getMessagesRequest(function: getUsernameRequest, anotherFunc: performSegueToChat)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM