"let task = session.dataTask(with: urlRequest) "如何工作并返回值?



继我的最后一个问题(谢天谢地,我得到了帮助(之后,我正试图让这一小段代码为我返回一个值,以便将其插入故事板上的文本字段。代码正在工作,但我不知道如何返回我需要的值。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()
}

该程序在控制台上显示数据,正常

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返回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()
}

Btw:为什么使用局部变量btn_balancemyBalance?我猜你指的是selft.btn_balanceself.myBalance。如果是这样,您还应该只在DispatchQueue.main.async闭包中写入这些值。

最新更新