首页 文章

“let task = session.dataTask(with:urlRequest)”如何工作并返回一个值? [重复]

提问于
浏览
-1

这个问题在这里已有答案:

再说我的上一个问题,谢天谢地我收到了帮助,我正在尝试使用这一小段代码为我返回一个值,以便我可以将它插入故事板上的文本字段 . 代码正在运行 - 但我无法弄清楚如何返回我需要的值 . myBalance或btn_balance .

我尝试过“返回值”,但它忽略了它 . 这是我修改过的代码 . 这会在文本字段中打印“Hello”,但不会打印值 . 我在这方面有点像诺贝尔,我担心自己已经深入到了一个深层 . 我可以在正常会话中返回函数中的数据,但是这个“任务”让我被打败了 .

class GetBalanceViewController: UIViewController {

var myBalance :String = ""
var btn_balance :String = ""

@IBOutlet weak var displayBalance: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    //makeGetCall()

    //display the balance on the screen
   // print(makeGetCall(myBalance: btn_balance))
    myBalance =  (makeGetCall(myBalance: btn_balance))
    print("Hello...: \(myBalance)") // blank
    displayBalance.text = (makeGetCall(myBalance: btn_balance)) + "Hello" // displays "Hello" 


    // Do any additional setup after loading the view.

}

功能 - 我的修改就是这个 .

func makeGetCall(myBalance: String) -> String  {
    // Set up the URL request
    let todoEndpoint: String = "https://api.jsecoin.com/v1.7/balance/auth/0/"
    //let todoEndpoint: String = "https://api.jsecoin.com/v1.7/ledger/auth/"
    //let todoEndpoint: String = "https://api.jsecoin.com/v1.7/balance/checkuserid/73276/auth/"
    let apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"


    guard let url = URL(string: todoEndpoint) else {
        print("Error: cannot create URL")
        return "Error"
    }
    var urlRequest = URLRequest(url: url)

    urlRequest.setValue(apiKey, forHTTPHeaderField: "Authorization")
    urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
    // set up the session
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    // make the request
    let task = session.dataTask(with: urlRequest) {
        (data, response, error) in
        // check for any errors
        guard error == nil else {
            print("error calling GET on /todos/1")
            print(error!)
            return
        }


        // make sure we got data
        guard let responseData = data else {
            print("Error: did not receive data")
            return
        }
        print("Got data")
        // parse the result as JSON, since that's what the API provides
        do {
            guard let todo = try JSONSerialization.jsonObject(with: responseData, options: [])
                as? [String: Any] else {
                    print("error trying to convert data to JSON")
                    return
            }
            // now we have the todo
            // let's just print it to prove we can access it
            print("The todo is: " + todo.description)

            // the todo object is a dictionary
            // so we just access the title using the "title" key
            // so check for a title and print it if we have one
            let index = todo.index(forKey: "notification")

            let btn_balance = (todo[index!].value)

            let myBalance = btn_balance
            print("myBalance I: \(myBalance)")
            print("btn_balance I: \(btn_balance)")

          /*
            for (key,value) in todo
            {
                print("\(key) : \(value)")

            }
           */
        /*
            guard let todoTitle = todo["balance"] as? String
                else {
              //  print("Could not get todo title from JSON")
                return
                }
         */
           //print("The title is: " + todoTitle)
            } catch  {
            print("error trying to convert data to JSON")
            return
        }

    }
   task.resume()
     return btn_balance
}

原来只是这个 .

makeGetCall()

func makeGetCall() {
.....
   }
   task.resume()

}

程序在控制台ok中显示数据

The todo is: ["notification": Your balance is 5646.65 JSE, "balance": 5646.65, "success": 1]
myBalance I: Your balance is 5646.65 JSE
btn_balance I: Your balance is 5646.65 JSE

所以这就是问题,我怎样才能获得这个值,我可以看到,回到故事板 .

1 回答

  • 1

    由于 dataTask 异步工作,因此您不能简单地从 makeGetCall 返回值 . 相反,您必须更新闭包内的数据模型或UI .

    您可以使 makeGetCall 返回 Void ,并在完成处理程序中添加 DispatchQueue.main.async 调用,以更新UI(可能还有更多内容,例如更新属性) .

    就像是:

    func makeGetCall(myBalance: String) -> ()  {
        // ...
    
        let task = session.dataTask(with: urlRequest) {
            (data, response, error) in
    
            // ..
    
            do {
                // ...
    
                DispatchQueue.main.async {
                    if let balanceString = todo[index!].value as? String {    
                        self.btn_balance = balanceString
                        self.displayBalance.text = balanceString
                    } else {
                        // Ooops
                        self.displayBalance.text = "?? unknown ??"
                    }
                }
                // ...
            }
    
        }
       task.resume()
    }
    

    顺便说一句:为什么你使用局部变量 btn_balancemyBalance ?我猜你的意思是 selft.btn_balanceself.myBalance . 如果是这样,您还应该只在 DispatchQueue.main.async 闭包内写入这些值 .

相关问题