URLStringConvertible and Requests

In this lesson

In Alamofire, you can use many different object types to define your destination URLs for your requests all with the help of the URLStringConvertible. You can even add your own custom extensions to add this feature to your models.

Kyle Roberts
Swift Guru at Large

Kyle's Series


Tap on time to skip ahead


Hello world, Kyle here brax.tv and we’re talking about the Alamofire request URL parameter in the basic request initialization method. Alamofire.request. What’s cool about the URL parameter of the request method here is that it’s actually of type URLStringConvertible and the types that initially come supported with Alamofire are NSString, NSURL, NSURLRequest, NSURLComponents, and of course, just the standard Swift string, which it does not show as an example here. 


But what’s great about this is that any of these ways that you can define a URL as a string or any of these different NSURL semi-related object types are usable for the URL parameter in the request method here. Now for the example that it doesn’t show in the Alamofire documentation on their GitHub is the Swift string but I can show that in my example app here called SteamReader and we are in the NetworkInterface.swift file. And for every network action I have defined in this file, I’m actually just using the Swift string as the argument to this method. And it’s as simple as pasting the URL into a pair of double quotes. 


And we can Command+click into this request method and then Command+click into URLStringConvertible here just to view what is supported straight out of the box with Alamofire. And here, again, is basically what we saw in the documentation. Another thing that’s cool about this type, this URLStringConvertible type, that is also pointed out in the documentation is that it is, at least it says it’s simple enough to create your own custom types that can subscribe to this URLStringConvertible type. 


Maybe for some reason you’re needing to work with a custom type, here it says as a convenient way to map domain specific models to server resources, kind of some mumbo jumbo unless you really need to do this. The definition of that is super super easy and you can just copy this in, even within your own code outside of whatever class you’re working in and just change this type to a custom type. 


Maybe a great idea I just had is I could maybe even replace, you know, my fetchAppDetails request here. And let’s say this is AppDetails and we’ll extend and details witht his URLStringCompatible type and we could even just return a static string here which is the global HTTP in point for fetchingAppDetails in SteamReader. And of course which appDetails we get are defined it within the URL parameters that I’ve added here. To run this, we could actually just pass in app.details and the convertible up here will return the correct string that we’re looking for. I just wanted to illustrate that the URL parameter of this request meant that actually takes the URLStringConvertible, which means you can use the out of the box supported type or you can come up with your own custom types. 

Additional Info

Register to get access to additional resources and info.