尝试从全局函数传递值

问题描述 投票:0回答:1

我试图从另一个视图调用的函数传递 Double 值,并且该函数在 AlamoFire 请求完成之前返回一个值

struct GetPrice {

    static func getThePrice(book: String) async -> Double? {
        
        var theNetPrice: Double = 0.0
        var iListPrice = ""
        let url = "https://example.com"
        
        let headers: HTTPHeaders = [
            "accept": "application/xml",
            "Authorization": "Basic \xxx",
           
            "Content-Type": "application/x-www-form-urlencoded"
        ]

        let body: [String: Any] = ["xxxxx"]

        AF.request(url, method: .post, parameters: body, encoding: URLEncoding.default, headers: headers)
            .responseData { response in
            var statusCode = response.response?.statusCode
                //print("response.result ", response.result)
            switch response.result {
            case .success:
                let data = response.data
                let s = String(data: data!, encoding: .utf8)
                let xml = XMLHash.parse(String(s!))
                
                    iListPrice = xml["xxx"].element!.text
                    theNetPrice = Double(iListPrice)
                    print(theNetPrice) // this shows correct value
                    //return theNetPrice -- this would give error 'Cannot convert value of type 'Double' to closure result type 'Void''
            case .failure(let error):
                statusCode = error._code
                print("status code is: \(String(describing: statusCode))")
                
                print(error)
            }
        }
        
        return theNetPrice //returns 0.0
    }
}
'''
asynchronous swiftui async-await
1个回答
0
投票

使用

continuation
尝试此方法,如示例代码所示。

struct GetPrice {

    static func getThePrice(book: String) async -> Double? {
        //..
        return await withCheckedContinuation { continuation in
            AF.request(url, method: .post, parameters: body, encoding: URLEncoding.default, headers: headers)
                .responseData { response in
                    var statusCode = response.response?.statusCode
                    //print("response.result ", response.result)
                    switch response.result {
                    case .success:
                        let data = response.data
                        let s = String(data: data!, encoding: .utf8)
                        let xml = XMLHash.parse(String(s!))
                        
                        iListPrice = xml["xxx"].element!.text
                        theNetPrice = Double(iListPrice)
                        print(theNetPrice) // this shows correct value
                        continuation.resume(returning: theNetPrice)
                    case .failure(let error):
                        statusCode = error._code
                        print("status code is: \(String(describing: statusCode))")
                        print(error)
                        continuation.resume(returning: theNetPrice)
                    }
                }
        }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.