Ios “怎么说?”;让task=session.dataTask(带:urlRequest)";工作并返回值?

Ios “怎么说?”;让task=session.dataTask(带:urlRequest)";工作并返回值?,ios,swift4,Ios,Swift4,关于我的上一个问题,谢天谢地,我得到了帮助,我正在尝试让这段代码为我返回一个值,这样我就可以将它插入到故事板上的文本字段中。代码正在运行-但我无法确定如何返回所需的值。我的余额,或btn_余额 我尝试了一个“返回值”,但它忽略了它。 这是我修改过的代码。这会在文本字段中打印“Hello”,但不会打印值。我在这方面有点不知所措,恐怕我已经有点深陷其中了。我可以像在正常会话中一样从函数返回数据,但这个“任务”让我失败了 class GetBalanceViewController: UIViewCo

关于我的上一个问题,谢天谢地,我得到了帮助,我正在尝试让这段代码为我返回一个值,这样我就可以将它插入到故事板上的文本字段中。代码正在运行-但我无法确定如何返回所需的值。我的余额,或btn_余额

我尝试了一个“返回值”,但它忽略了它。 这是我修改过的代码。这会在文本字段中打印“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

这就是问题所在,我如何才能得到这个值,正如您所看到的,返回到故事板。

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

您可以进行
makeGetCall
return
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_balance
myBalance
?我猜你的意思是
selft.btn\u balance
self.myBalance
。如果是这样,您也应该只在
DispatchQueue.main.async
闭包中写入这些值。

对不起,我不太明白。我回到了原来的调用super.viewDidLoad()makeGetCall(),我得到了错误。模块“Dispatch”没有名为“main”的成员您需要帮助吗?在
viewDidLoad
中,调用
makeGetCall
。由于没有返回值,因此不在此处设置任何
displayBalance.text
。这是在
Dispatch.main.async
中的
session.dataTask
中完成的。是的,我现在知道了,但是我得到的模块“Dispatch”没有名为“main”的成员,因为我将您的代码放在func makeGetCall()Arrg的主体中,这是我的错。是
DispatchQueue
…好的,谢谢。我有一个想法,一定是从搜索stackoverflow得到的。然而现在我得到这个DispatchQueue.main.async{self.btn_balance=(todo[index!].value)self.displayBalance.text=btn_balance}错误:无法根据btn_balance和displayBalance将类型“Any”的值分配给类型“String”。叹气