Check out a free preview of the full iOS App Development with Swift course:
The "Adding Alamofire Package" Lesson is part of the full, iOS App Development with Swift course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano walks through adding and importing a dependency called Alamofire which is used to download JSON from an API.

Get Unlimited Access Now

Transcript from the "Adding Alamofire Package" Lesson

>> Let's see, if I run this on the real thing, okay, we have products, we have offers. We have the order that says it's empty, okay? So this is kind of working so far at this point, okay? Details is the one that is also showing any dummy product and not the real ones.

[00:00:26] So the next step, just to finish wiring this, all the missing points is to download the JSON, okay? It will be interesting to do that. And for that, the iOS provides a class, an API, to actually access a native way to download files. But instead of using that native API, we're going to use a framework.

[00:00:53] This is like the framework that every Swift developer is using to use in the network. It's called Alamofire. And also it's a good opportunity to use one dependency, so we can actually see how that works. So if you go to the companion website using APIs and JSON, in the first example, you have the step.

[00:01:15] It's not a big deal, but you have the link to Alamofire, the website in case you wanna get more information about how that works. And also there is a URL. In fact, it's a Git file in GitHub. So I will copy that to my clipboard. And to add a dependency, an external dependency, I will go to the Project Explorer, right-click in my project, and I will select Add Packages.

[00:01:50] Okay, so you right-click in the project, Add Packages. And here, I can paste the URL. So every open source project for Swift, every dependency, you will have the link to actually use it, okay? So I'm pasting the GitHub URL of Alamofire here, and I will pick Add Package.

[00:02:18] This is like NPM install, if you want, okay, compared with JavaScript. So if I click Add Package, it's going to fetch the last version of the library. Finally, we hit Add Package, and we are done. Now we can use it, and the dependency appears here at the bottom of the project, okay?

[00:02:42] You see, we have Alamofire.
>> Is there any versioning on that?
>> You know how in package.json, it adds something that says, this is my dependency and my version.
>> We have something similar here. So if you go to the dependencies section or the package, when you add the package also.

[00:03:00] When you are answering the version number, here, you can actually select the version number. An exact version, by default, is downloading the latest one. But yeah, you do have also. If you right-click on your project and you click Show in Finder, you will see the real file system.

[00:03:21] And in the real file system, okay, you will find some configuration files similar to package.json. I'm not saying that you wanna deal with that or you wanna play with that. But also, if you click in the project and you are in General, if you scroll down, there is a section for frameworks, libraries, and embedded content.

[00:03:49] And here, we have Alamofire. So if I remove that, you can still do that, okay? Alamofire, it's actually pretty simple. It works like the Fetch API in JavaScript. You just call a function, and then you get the data. As simple as that in a callback. So it's actually pretty, pretty simple to use, okay?

[00:04:11] So in our MenuManager, actually we can add the code here to actually download the data, okay? Here if you go to the next section, the code, you can see it's actually simple to understand, okay? So I create a function, That is calling AF, Alamofire. But in this case, Alamofire is an external framework.

[00:04:47] So it's the first time we actually need to import something, import Alamofire, okay? And Alamofire has a request function, similar to fetch or jQuery, Ajax. If you want, you pass the URL, and then you need to specify here. The weird part here is this one. You need to specify what are you expecting in terms of data type from the JSON.

[00:05:18] This is going to parse the JSON automatically and it will map the JSON to my data model, okay? Automatically, it will match that. So how do you pass a type as an argument? The type .self, so array of category .self, it's saying, I'm expecting you to return the array of categories.

>> It has to be matching exactly everything, I mean, can you-
>> No, you don't need a one-to-one match.
>> Subset has to be-
>> It can be a subset. Also, for a more advanced situation, you can map. Yeah, you can specify, okay, on the JSON, this is the key name.

[00:06:06] On my structure, this is the variable name. So yeah, you can actually customize that, sure. So in this case, look at the code. So I have a function refreshItemsFromNetwork that makes a request. And then it says array of categories. And then if it's there, because maybe there is an error or, I don't know, something happens, I'm saving that as my menu.

[00:06:37] And because it's published, if everything works, it should automatically appear with wherever binding you have. The only error that we have is that it's complaining about something that says, category is not conforming to decodable. And what's that? If you want your data model to automatically be able to be converted from and to JSON and other formats, but let's say JSON, it must conform to a protocol that is called decodable.

[00:07:16] With decodable, it can be decoded. You also have encodable, to encode. And if you want both at the same time, you do have codable. That is a type alias, actually, that applies both decodable and encodable. So it's an alias to the type, okay? But for now, we just need decodable.

[00:07:39] With that, for what we are doing here, decodable is fine. And because category has an array of product, product needs also to be decodable, okay? So we just need to add the protocol name, that's all. We don't need to write any code by default, okay? With that, it means that it will just work.

[00:08:06] Let's see if it compiles, it compiles. By the way, how I'm compiling so quickly? Cmd+B, it's building the project. So I know if it's compiling or not. And something that I can do is when I'm initializing the MenuManager, I can just call refresh. So when the app starts, we go to the network and download the data.

[00:08:28] Again, this is going to the network, downloading the JSON file, and automatically mapping the JSON that is coming from the network to my data model, okay? Does it make sense? You see if it's working right now before actually seeing it in action, we can print response.value and see what we see, okay?

[00:08:51] So if I run this in the emulator, here, if you look at the console, we have a JSON output. And if it's not easy to read here, we can always go to the output navigator, it's this one. And this is the one that we are looking for. And if we drive, you have here the JSON.

[00:09:18] So it's actually downloading the JSON from, you see here, nonfat milk, blended with blah, blah. This is kinda the data, okay? This is actually the data.
>> How did you get it to run?
>> Here?
>> refreshItemsFromNetwork, you've got it to run somehow.
>> Because it's an init.

[00:09:37] And you say, who is calling init? Well, init is a constructor, so it's actually being called automatically. And who is creating the MenuManager? We did that at the app level. Not sure if you remember that. Here, we are creating the MenuManager. When we are creating the instance, the initializer is being executed.