CocoaPods Subspecs

In this lesson

In CocoaPods, there is a thing called subspecs that you can add to your podspec. A subspec is the definition of submodules of a library. Oftentimes, there is a core module that is included with any reference of the pod by default, with other subspecs or modules including other things that are not required for the basic usage of a library. For example, a library may have a subspec for a database in the cloud, or a subspec for authentication, while the main or most basic features of the library are within a default subspec.

Kyle Roberts
Swift Guru at Large

Kyle's Series

Transcript

Tap on time to skip ahead

0:11

Hello world, Kyle here with brax.tv and we’re going to talk about using subspecs in a podspec. As a user of CocoaPods, you may have seen before, some sort of syntax like this. And I’m just gonna make up a pod name because it’s an example. And that pod name I’m using will be SteamReader, the name of the app that I’m working in and the repo that you can find all of my example code for these videos. 

0:40

So in a podfile, as a user, if you’ve ever seen or had to use syntax like this, where you are referencing a pod, but they’re sort of a submodule to that pod. And that submodule is defined by just having a slash between the pod name and then the submodule name. And that’s a way where you install something called a subspec. And that’s basically just when a CocoaPod has several modules that you can download and use but you don’t necessarily have to use them all at once. 

1:14

So another example is maybe there’s another subspec to this SteamReader CocoaPod called Network. So in this case, according to these two lines of examples here, in this podfile, I’m going to download the Core and Network modules of the SteamReader CocoaPod. 

1:31

So how do you define that in a podspec? Well, here I have the SteamReaderExample.podspec, which is another example file that you can find within the SteamReader repo. And scrolling all the way down to the bottom to this subspecs section that I’ve created, we can see some examples subspecs. So here I have defined three different subspecs. So the basic subspec definition configuration within a podspec is like this:

2:00

You take your original reference to your entire podspec, in this case, mine’s just called it s for spec, and type .subspec, space the name of the submodule of your CocoaPod, and I’m just using CoreData as a submodule  of the SteamReaderExample.podspec just for example. And you’re going to wrap the name in quotes, do and then name your reference to the subspec surrounded by vertical lines, and I’m just name that reference sub. In here, we’re setting the subspec source files to a different path of source files than what I was setting up here. And this path, again just an example, it’s not a real path in the SteamReader repo, but of course with your subspec going to want to point it to some different code. 

2:51

So below that, after I fix a couple of these typos, I’m defining another suspect called Network. And within that I’m setting some different source files. And a unique property of a subspec that is not on a standard podspec object is the dependency property. I’m actually pointing this Network submodule’s subspec to have a dependency on the CoreData submodule subspec. And you do that just by wrapping the standard podfile subspec reference in quotes. So that if a user were to install only this Network subsspec, then they would also be installing the CoreData subspec because it requires that dependency. 

3:37

And again, just making up another path for the source files and actually with in that subspec, you can add even sub-er subspecs and have sub-subspecs or, you know on and on, I don’t wanna say sub a billion times. And because I don’t say sub twice, I’ve actually named this sub-subspec nested. And we’re defining that just the same as we were with the others but instead of on the top level podspec reference, I’m actually using this sub reference here.

4:11

And within that, just setting some new source files again. Easy as that. One property of a podspec that is not covered in any of these other sections here is setting the default subspecs on your podspec. And you do that just by referencing the submodule name there. And you are actually able to pass in more than one like that. And if we wanted to reference that subspec, you would do you as such! And really just to make sure that your subspecs actually have content, it’s mostly in pointing the source files to a different directory of code. And there’s subspecs for you! 

Additional Info

Register to get access to additional resources and info.