Unexpected non-void return value in void function Swift3

The problem is that you are trying to return a non-void value from inside a closure, which only returns from the closure, but since that closure expects a void return value, you receive the error.

You cannot return from an asynchronous function using the standard return ... syntax, you have to declare your function to accept a completion handler and return the value from the async network call inside the completion handler.

func findChat(string: String, completion: @escaping (Chat?)->()) {
    var returnValue: (Chat?)
    let url = getChatsURL
    let Parameters = [ "title" : string ]

    Alamofire.request("\(url)", method: .post, parameters: Parameters).validate().responseString { response in
        if let anyResponse = response.result.value {
            self.responseFromServer = anyResponse
        }
        if self.responseFromServer == "" {
            completion(nil)
        } else {
            let ref = DatabaseReference.chats.reference()
            let query = ref.queryOrdered(byChild: "uid").queryEqual(toValue: (self.responseFromServer))
                query.observe(.childAdded, with: { snapshot in
                completion(Chat(dictionary: snapshot.value as! [String : Any]))
            })
        }
    }
}

Then you can call this function and use the return value like this:

findChat(string: "inputString", completion: { chat in
    if let chat = chat {
        //use the return value
    } else {
        //handle nil response
    }
})

Leave a Comment