Further to my last question, which thankfully I received help with, I'm trying to get this little bit of code to return a value for me so I can plug it into the text field on the storyboard. The code is working - but I can't work out how to return the value I need. myBalance, or btn_balance.
I've tried a "return value" but it's ignoring it. This is my modified code. This prints "Hello" in the text field, but not the value. I'm a bit of a noobie at this and am afraid that I've jumped into the deep end a bit. I can return data from a function as in normal sessions, but this 'task' has me beaten.
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.
}
The function - with my modification, is this.
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
}
The Original is simply this.
makeGetCall()
func makeGetCall() {
.....
}
task.resume()
}
The program displays the data on in the console 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
So that's the question, how can I get that value, which I can get as you see, back to the storyboard.