这个问题在这里已有答案:
再说我的上一个问题,谢天谢地我收到了帮助,我正在尝试使用这一小段代码为我返回一个值,以便我可以将它插入故事板上的文本字段 . 代码正在运行 - 但我无法弄清楚如何返回我需要的值 . 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 回答
由于
dataTask
异步工作,因此您不能简单地从makeGetCall
返回值 . 相反,您必须更新闭包内的数据模型或UI .您可以使
makeGetCall
返回Void
,并在完成处理程序中添加DispatchQueue.main.async
调用,以更新UI(可能还有更多内容,例如更新属性) .就像是:
顺便说一句:为什么你使用局部变量
btn_balance
和myBalance
?我猜你的意思是selft.btn_balance
和self.myBalance
. 如果是这样,您还应该只在DispatchQueue.main.async
闭包内写入这些值 .