Decode JSON pulled from Web API Swift Xcode Tutorial - Crypto Price Tracker App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the swift xcode tutorial my  name's cal and in today's tutorial we're going to   be pulling data from the web decoding it from  its json format and then displaying it without   our app in this example project i've decided  to use bitcoin we're going to just be pulling   some current information about what the price  of one bitcoin is in us dollars ethereum   and australian dollars but really  this is just an example and the   principles applied in this video  can be used for any web service cool so new xcode project ios app i'm going to  call this bitcoin tracker tutorial storyboard   and swift is our programming language firstly  we're just going to drag in a few images so i'll   leave a link in the description to these images  if you'd like to use these exact ones but i just   pulled them off the web heading into the main  storyboard now we're going to drag in an image   view onto our storyboard select our bitcoin image  we're going to right click and drag onto itself   to give it some constraints giving aspect ratio  as well as height and then we're going to right   click and drag from the image onto the view giving  it leading space and top space and now we're just   going to head in and modify those constraints  we're going to set a line leading to 20 height   equal to 70 our aspect ratio is going to be one  to one and next what we want to do is just drag   in a label just to the right of our image we're  going to make the font size a little bigger we're   going to set horizontal spacing as well as center  vertically off our image and then just changing   our leading space to 20 and then by holding down  command i can select both the image and the label   i'm going to hit command c and command v to just  copy and paste and then we just need to redo the   leading space as well as the top space to the  above button and we're just going to do that   another three times so we're going to have four  rows of images with a label connected to it so   leading space and top space off the above button  and then we just want to go through and change all   of the images so the next one's going to be the  ethereum icon and then we're going to have the us   flag followed by the australian flag and then we  just want to copy and paste one more label we're   going to put it in the center of our safe area  and just changing it to last updated cool so we   can open up the assistant editor now i'm just  going to create some outlets so the first one   we're going to call btc price so bitcoin price  and then we're going to right click and drag   create now let for our ethereum price same  thing for our us dollars price and one more   for our aud price and then we're going to right  click and drag from our last updated and create   an outlet cool so we can close off this assistant  editor now and head into our main view controller   and firstly what we're going to do is just copy  and paste in this url string so this is the api   we're going to be pulling the data from and  you can see it's just a pretty basic url   if you copy and paste that url into a web browser  we get this big slab of information in json format   in the interest of keeping this tutorial short  and simple what i've done is i've copy and pasted   all this information into a text editor i've  formatted the json and then i've just deleted   all of the things that aren't relevant so for  this tutorial we only care about the price of   four of the currencies so what i'm going to do  is copy and paste that into our view controller   swift file so we've got something to reference we  know that that's the format of our json and the   next step is to basically just model the current  json data and turn it into some objects to achieve   this we're just going to declare a struct which  inherits from the class codable our first struct   we're basically going to start with the innermost  data so we're going to have a struct with name   unit value and type and you can see that name unit  and type are all strings as they are nested in   commas however value there doesn't have any  commas and it does have a decimal place so   we're going to set our value to be type float  cool so moving our way up the hierarchy we can   see the next values are btc and so on so we're  going to create another object to hold all of   these different prices we're going to name it  currency inside our currency let btc equal to   our price object and then we're going to create  one for eth usd and aud and then we just need to   create one more so the top one which is our rates  and our rates object is just going to hold all of   our currencies cool so now that we've modeled  the data we now need to fetch the data so let's   just create a function and just call it from the  view did load we're going to say let url creating   a url from our url string and then we're going  to say let default session equal to url session   configuration is default and then we're going to  say let our data task equal to our default session   i'm going to say data task and we're going to  select this one with url and a completion handler   we're going to pass it in our url as our url and  our completion handler we're going to give our   data variable a name of data our response a name  of response and an error we're just going to call   it error and that error message is just saying we  need to force unwrap our url which we're going to   do and before we continue on with our data task  we're just going to say data task resume which   will start the data task if it's not currently  running cool so next what we're going to do is   just check to see if there were any errors so  we're going to say if error is not equal to nil   we're going to print that error and then return  and then below that meaning that there's no error   we're going to try and initialize our json object  inside a do try catch statement so just saying do   let json equal to try json decoder decode from  our rates so that's our rates struct from our data   which was passed in through our completion handler  and then we're just going to catch any errors   print them out if we found them and then return  as well cool so assuming that that json object is   now populated what do we want to do we want to set  our view to have all the different prices so we're   just going to create a function calling it set  prices which we're going to call beneath our json   and our set prices is going to receive a currency  object so we're just going to say self.set prices   and passing it in our json.rates and then below  our set prices we're going to create another   function we're going to call it format price our  format price function is just going to receive a   price and return a string and so we're just going  to say return string format our string format is   going to be a string and then a float to four  decimal places and then we're just going to pass   in our price unit which you can see here would be  btc and then a price value which for ethereum's   case would be 14.5 and then we're just going to  say from our set prices we're going to say self   btcprice txt is equal to self.format price and  giving it our currency of btc and then we're just   going to do the same for our other four currencies  so just ethereum usd and aud just passing in the   correct currency and then below that we're going  to say self last updated price which really should   just be last updated we're going to set its text  to we're going to create another function calling   it format date so we just want to get today's  date or the current date and so we're going   to say formatter is equal to date formatter  formatted date format is equal to days months   years hours minutes and seconds and then we're  going to return our formatter string from date   and now we can say format date giving it a new  date so today's date or the exact current time and   as we're trying to access or change the view from  not the main thread we just need to add all of our   statements inside this dispatch queue main async  and that's going to remove that error message cool   so let's go and run this now and we can see that  let's run there and it's given us a time at 526   it's given us the current price of one bitcoin  cool and then i'll quickly just show you how   you can make this sort of update every so often so  i'm just going to say let timer equal to schedule   timer at time interval 10. so every 10 seconds our  target is going to be self our selector is going   to be an objective selector so i'm just going to  type refresh data user info is nil and repeats is   true and then we just need to create that function  so refresh data just calling it an objective c   function which returns void and if we build and  run this again and we pay close attention to the   bottom time string should be updating every 10  seconds if you liked this with the swift and   xcode tutorial consider liking the video  otherwise i'll catch you guys in the next one
Info
Channel: Code With Cal
Views: 636
Rating: 5 out of 5
Keywords: swift, xcode, tutorial, storyboard, rest, json, web, api, pull, request, parse, bitcion, bitcoin, app, price, data, from, learn, btc, eth, usd, dollars, exchange, rates, live, current, info, GET, htttp, http, coin, gecko, web service, 2021, crypto, Aus, USD, AUD, finance, application, bit, format, date, website, webpage, display, in app, external, exact, prices, beginner, code, with, cal, app dev, iOS, development, dev, develop, create, build, apps, simple, project, idea, begin, start, easy, to, follow, along, australia, program, programming, apple, example, appl
Id: 50-8pGoRjo0
Channel Id: undefined
Length: 7min 25sec (445 seconds)
Published: Sat Aug 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.