I trying return JSON Dictionary from Alamofire. The problem is that when assigning JSON to an external variable, the assigned value is saved and read only inside the Alamofire.request block, and outside its value is not saved (magic for me). What have I done wrong?
I do this first time. I'm tried use URLSession.shared.dataTask but I also could not return value from task.
I noticed that first the breakpoint triggered on the return res line, then on the return json line and then on the print (JSON) line. I do not know why.
struct resultMsg {
    var message: [String:Any] = [:]
    init() { }
    init(message: [String:Any]) {
        self.message = message
    }
}
func sendGET(method: String, data: [String:String]) -> [String:Any] {
    let resultURL: String = rootURI + method
    let url = URL(string: resultURL)!
    var res: [String:Any] = [:]
    Alamofire.request(url, method: .post, parameters: data, encoding: JSONEncoding.default)
        .responseJSON { response in
            print(response)
            if let status = response.response?.statusCode {
                switch(status){
                case 201:
                    print("example success")
                default:
                    print("error with response status: \(status)")
                }
            }
            //to get JSON return value
            if let result = response.result.value {
                let JSON = result as! NSDictionary
                res = JSON as! [String : Any]
                print(JSON) // Here res is contain actualy json
            }
    }
    return res // Here res equal [:]
}
public func Authorize() -> resultMsg {
    let data: [String:String] = ["login":login,
                                 "pass":pass]        
    var json = resultMsg.init()
    json.message = sendGET(method: "auth.php", data: data)
    return json
}
 
     
     
    