Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Austin Murtha
4,843 PointsGetting return from JSON request Swift
I am trying to add a JSON request to my app, and get back one value. The query is working, but I am not sure how to get the value out of the function.
1 Answer

Unsubscribed User
11,496 PointsHi Austin,
If you've defined it as a property in a view, when you run your function it should update the value of exchangeRate and you can access it from the class name:
yourViewController.exchangeRate or self.exchangeRate.
If you're trying to set a label's text equal to your result, you could just replace the end bit of your code where you set exchangeRate = multiplierRate with:
self.exchangeRateLabel.text = multiplierRate
That would update the label every time you ran the function. (Don't forget to create an @IBOutlet for the label)
Austin Murtha
4,843 PointsAustin Murtha
4,843 PointsThanks Grant. I am looking to just get the Variable from json and pull it from the function.
The actual calculation was going to be done somewhere else. However, I do the calculation in the function.
Basically I give the user several currency options, and depending on what they choose it sets the json query. Then I get the value based on the options selected and pass that to the calculation sections.
Unsubscribed User
11,496 PointsUnsubscribed User
11,496 PointsIf you're defining exchangeRate as a class property, once you run your getCurrencyData function, exchangeRate will have its value set to the JSON value. At that point you just need to reference exchangeRate any time you want to get at that value. If you have some calculation function that you want to use it in, you can pass exchangeRate as an argument:
var adjustedPrice = doCalculation(exchangeRate, exchangeAmount)
You'll need to make sure your calculation function accepts a Double as one of its arguments:
Also, you might want to look at https://github.com/SwiftyJSON/SwiftyJSON#usage Parsing JSON in Swift is pretty ugly and using SwiftyJSON would clean up your function a bit!
Austin Murtha
4,843 PointsAustin Murtha
4,843 PointsGreat minds link alike. I have been doing the swiftyJson tutorial since I ran into this issue. let me try would you have described below.
thanks
Austin Murtha
4,843 PointsAustin Murtha
4,843 PointsSo when I added return multiplieRate I get an error on line 20 that says double not convertible to void. i believe this is due to the completion handler, but not sure how to resolve.
If I add remote mutiplierRate on line 29 it says unresolved identifier multiplierRate
https://gist.github.com/austinmurtha/09ff3947a3f8417d821d
Thanks
Austin Murtha
4,843 PointsAustin Murtha
4,843 PointsSo when I added return multiplieRate I get an error on line 20 that says double not convertible to void. i believe this is due to the completion handler, but not sure how to resolve.
If I add remote mutiplierRate on line 29 it says unresolved identifier multiplierRate
https://gist.github.com/austinmurtha/09ff3947a3f8417d821d
Thanks