如何从异步块返回值?

时间:2017-04-28 10:14:58

标签: ios swift asynchronous alamofire grand-central-dispatch

这是我想要返回值的函数但是我知道我从一个库中使用的闭包中返回值并且它返回void我觉得很傻,我认为它是async void block,我是新的在它请帮助。 :(

   func xmlRequest(_ id:Int) -> String{
            var array1:Array<Any> = Array<Any>();
            array1.append("body" as Any);
            array1.append("date" as Any);
            array1.append("author_id" as Any);

            let con:NSDictionary = NSDictionary();
            var field2:Array<Any> = Array<Any>();
            field2.append([["id", "=", [id]]]);
            let url = Login.BASEURL+XMLRPC_SERVICE_OBJECT;
            let param3 = [Login.DATABASE, Login.LOGIN_ID, Login.PASSWORD, "mail.tracking.value","search_read",field2,con] as [Any];
            AlamofireXMLRPC.request(url, methodName: "execute_kw", parameters: param3).responseXMLRPC { (response: DataResponse<XMLRPCNode>) in
                switch response.result {
                case .success(let value):
                    return "success";
                case .failure(let error):
                    return error;
                }//switch end
            }
        }

如果我尝试返回值,则会出现以下错误。 enter image description here

func xmlRequest(_ id:Int) -> String{
        var array1:Array<Any> = Array<Any>();
        array1.append("body" as Any);
        array1.append("date" as Any);
        array1.append("author_id" as Any);

        let con:NSDictionary = NSDictionary();
        var field2:Array<Any> = Array<Any>();
        field2.append([["id", "=", [id]]]);
        let url = Login.BASEURL+XMLRPC_SERVICE_OBJECT;
        let param3 = [Login.DATABASE, Login.LOGIN_ID, Login.PASSWORD, "mail.tracking.value","search_read",field2,con] as [Any];
        var valueToReturn:String = "default"
        AlamofireXMLRPC.request(url, methodName: "execute_kw", parameters: param3).responseXMLRPC { (response: DataResponse<XMLRPCNode>) in
            switch response.result {
            case .success(let value):
                valueToReturn = "success"
            case .failure(let error):
                valueToReturn = "error"
            }//switch end
        }
        return valueToReturn;
    }

我也尝试这个黑客,但它总是返回默认值,因为授权中央调度在其中工作的东西,我再次说我是新的ios请帮助:(

2 个答案:

答案 0 :(得分:1)

您可以通过完成关闭,并在成功或失败请求后执行它。

func xmlRequest(_ id:Int, completion: @escaping ((String) -> Void)){
    var array1:Array<Any> = Array<Any>();
    array1.append("body" as Any);
    array1.append("date" as Any);
    array1.append("author_id" as Any);

    let con:NSDictionary = NSDictionary();
    var field2:Array<Any> = Array<Any>();
    field2.append([["id", "=", [id]]]);
    let url = Login.BASEURL+XMLRPC_SERVICE_OBJECT;
    let param3 = [Login.DATABASE, Login.LOGIN_ID, Login.PASSWORD, "mail.tracking.value","search_read",field2,con] as [Any];

    AlamofireXMLRPC.request(url, methodName: "execute_kw", parameters: param3).responseXMLRPC { (response: DataResponse<XMLRPCNode>) in
        var valueToReturn:String = "default"
        switch response.result {
        case .success(let value):
            valueToReturn = "success"
        case .failure(let error):
            valueToReturn = "error"
        }

        completion(valueToReturn)
    }
}

有关您可以在Apple文档中阅读的闭包的更多信息=)

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

答案 1 :(得分:1)

使用Closures从异步任务返回值。我修改了你的代码,你可以尝试一下,我没有编译它只是为了你的参考而编写。

call