Processing Data
Start course

In this course, we're going to build a currency converter app in order to teach you about JSON and APIs. We're going to get some data from the internet, process them, and then display them to the user.

Intended Audience

This course is designed for anyone who wants to:

  • Learn about iOS development and coding
  • Move into a career as an iOS developer
  • Master Swift skills


To get the most out of this course, you should have some basic knowledge of iOS.


Hi. Within this lecture we're going to see how we can get the values that we want from this jsonResponse so that we can finalize our app. So, what we want is to have a dictionary in here so that we can get the values that we want. And this time if you write jsonResponse you will see that this has the object type of any. So, first of all I don't want this to be any object. I want this to be a dictionary so that I can get whatever I want from out of this dictionary. And best way to do this actually casting this jsonResponse to be a dictionary. So, I'm going to cast this to be a dictionary of string to any. So, why am I doing this? Like can I do string to string or string to integer? No I cannot do that. I have to cast the string to any. Because re member if you go to our actual data you will see that this is a string. The keys are actually strings. But the values are not strings. So, this is a Boolean, this is an integer, so this is a string and this is actually a dictionary itself. So, we have every kind of objects in the value type. So, I cannot do a single value here. So, I have to do a string to any dictionary. So, if you're working with another API, another response, of course you can do something like string to string but in our case it's not string to string or string to integer, it's string to any. And once I do that I can try to get the values by providing some keys. For example, let me try to print the success of this jsonResponse. So, this is either going to be true or false or something like one or zero. So, let me print this out and we're going to see the result in the logs once we hit 'Get Rates'. And as you can see we see the one. So, this is optional right now but we're going to take care of that. And if this was a failure then I would have gotten zero, now it means that I'm getting this success. So, can I get this rates dictionary? So, I'm not interested in the timestamp based or date right now but I'm interested in the rates. So, this will give me a dictionary as well, right? And I'm interested in that dictionary. So, this is going to be the rates keys and the values of the rates. So, if I say rates over here it will surely print out the rates. So, I'm digging my way one by one until I reach the values that I want as you can see. So, here we go, now I have the rates. So, this is good. Now, all I have to do is just to create a structure that I can use this as a dictionary as well. So, this is kind of optional  right now and I cannot be sure of the types. So, I'm going to use the if let in order to cast this as a dictionary and we're going to see what we are getting clearly. So, I'm going to say if let rates and this is going to be jsonResponse rates. I'm going to cast this optionally as a string to any dictionary. And let me open curly braces in here. And this time this string to any actually can be something like string to double or string to integer. As you can see this kind of looks like a double but it may be a string as well. In order to understand this, I'm going to print the rates. So, let me print the rates and we're going to see the actual rates in here once we click on this 'Get Rates' button. And here you go. Now, as you can see the keys are strings and the values look like doubles rather than strings. Of course we can still test this and we can actually see if this works or not. The best way to do this, since we have this dictionary as a string to any, we can just call whatever we want. Whatever key, whatever value that we want and we can try to cast this as double or cast this as integer or cast this as string, then we can be sure of the type. Once we do that, we're going to display them in our labels as well. So, this is kind of the end of our app. So, I'm going to say if let and we can just start working with cad. Okay so this is Canadian dollars and this is going to be something like rates. I'm going to call this rates dictionary and in the key, I'm going to give CAD with uppercase letters of course. And I will try to cast this optionally as string first. So, this is going to be string and I will see if this works or not. So, let me try to print this cad and let me run this. And let's see if we can print the cad or not. So, the value looks like double to me. And as you can see once I hit the 'Get Rates' I don't see the result. So, let's try to do this like a double. And this time I believe we're going to see the result and then we can start working on our process with other values as well. As you can see now we have 1.48 in the cad. And in order to be sure you can go over this browser and look the value of cad. Yep, this is 1.48. So, this actually works. Now, you see the importance of studying your jsonResponse. As you can see we get the double and we have a string to any dictionary and we tried to cast this a string and it didn't work so now I had to cast this as double. So, if you're working with another API or another jsonResponse, you have to make sure that you adjust codes according to their structure. And in here, I'm going to say self.cadLabel.text and this is going to be cad, and since this gives me a double, I'm going to do this like this, string cad . So, all we need to do is just run this and let's see if we can display this cad value in the cad label. And here you go. So, this is working and this is not exactly what I want. Let me do it like this. Let me create a string over here and say CAD: and open this \(cad). And this will convert this into a string automatically so that we can have a nicer lo ok like this. Here you go now it's working. So, we're going to do this for other currencies as well. So, I'm going to copy and paste this six times, okay? So, let me go over here and paste all of this. Now, instead of cad I'm going to go for CHF. So, this is going to be my Swiss franc. And this is CHF with these big letters and this one as well. And in the third one I'm going to have GBP. So, this is GBP and GBP again. And this is Great Britain pounds. So, the fourth one is Japanese yen. So, JPY. So, this is again, JPY. Let me change all of this, JPY. And the fifth one USD. Yep, the popular one, USD. The USD, USD and again, the USD. So, here we go. Final one. We cannot say TRY because it will confuse it for try, so I'm going to call this Turkish. Okay, we can say TRY in here and here as well. But this will be Turkish. And we don't need the seventh one. So, I believe we are ready to test this. Let me run this and let's see if we can get all the data in here. Let's click on the 'Get Rates' and here you go we didn't get the data because I didn't change the label variables at all. So, in here we have to have chfLabel, gbpLabel, jpyLabel, usdLabel and this tryLabel. Now, let's try one more time and hopefully we will get this right this time. Let's run this in our simulator and hit the button and here you go now we have the rates. So, this is cool. Now, we are getting information from the internet and we're getting this from an API and we are parsing, we are processing the json data. And you can use this API, you can use this json techniques in other APIs, other projects, other platforms as well. And I believe you're not ready to go with something big like Facebook or Twitter at this point, but you can try something with like a weather API, some simple API like this. And after you complete this course I'm sure you're going to be ready to go ahead and work with some big APIs like Facebook or other ones. And you will actually be more capable of using Swift once you complete this course, like the advanced Swift section and other sections as well. And this will give you a big freedom in order to integrate data from the cloud server and use it on your own apps. And this is not even the only thing that we're going to be doing with the cloud service or Internet. We're going to learn the firebase, we're going to create our users, we're going to save data to a database, we're going to upload some image, we're going to download some images as well. And it will be much more clear to you to how to use cloud servers in your apps once we do that. But for right now, congratulations on your first Internet active app. So, let's stop here and continue within the next section.


About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics