2.3 Intro to Swift Package Manager
Because you are no longer writing Swift code on a Mac, you won't have the support of Xcode or some of the native libraries such as UIKit. That doesn't mean you are completely left out in the cold. Apple has released Package Manager along with Swift. Package Manager allows you to build your applications in a similar fashion to how Xcode does.
In this lesson, I'll show you how to structure your code for Package Manager.
1.Introduction2 lessons, 05:56
2.Get Started4 lessons, 32:10
3.Conclusion1 lesson, 01:12
2.3 Intro to Swift Package Manager
So in the previous lesson you saw how to do some basic stuff by setting up Swift and then being able to get into and access some Swift functionality from the the rebel and being able to compile a single source file. But what if that's not enough? What if I want to create a little bit more of an elaborate application with multiple source files and maybe some dependencies? Another libraries, or something like that. Well, in this lesson, I'm gonna show you the basic introduction into the world of the package manager, within Swift that I can use now, on Linux. Now, what is the package manager for? Well, it's really a way to create a logical structure, for your application and be enable to create and build either executable files or libraries and then have dependencies pulled into these applications from other places or be able to create some nice pieces of functionality that can be imported as a dependency into other applications. Now, there's a lot that can be covered in this particular section, but I'm simply going to give you the basics. If you head over to swift.org and you go over to projects on the left-hand side and take a look at package manager, it's going to go into a lot more detail on what these things are and how to use them and even some examples. Down here, but I'm simply going to show you how the package manager can also be used to set up some basic project structure for use that you can logically structure your applications. Now one thing to pay attention to is I haven't really mentioned Xcode within this course at all and that's because when you're using Swift on other platforms outside of a Mac or outside of iOS development, in general, you don't get to use Xcode. There is no Xcode for Linux or Windows or anything like that. It's simply a Mac product, so that being the case, you're gonna have to use other means. Other text editors or other things like that to be able to create your Swift applications. So, in this lesson I'm simply gonna show you how to use this package manager as a nice little feature of being able to kind of logically structure your applications to be able to handle those types of things. So now, once all that is taken care of, what I wanna do now is I want to create a basic folder structure, a basic directory structure that's going to allow you to start to create some applications using that structure. So I'm going to start by creating a folder called Applications. So I'll say make directory application, whatever you would like. And let's go ahead and go into there. And as you can see, there's nothing in there just yet. So let's change that. I'm gonna make a directory. I'm gonna call this rps. And you'll see why in just a moment. So now that I've done that, I wanna go into RPS, and I wanna clear this. Now, in order for us to create this structure, we need to follow a couple different guidelines. So the first thing is we need to have a special file in here, and this special file is called package.swif. Now, I'm not gonna go into terrible detail about what this is about, but this is really a place. Where you can define things like the name of this particular package that's gonna be associated with your executable, or a library that can be then referred to by other packages that you create, of being able to import this. This one, and then vice versa I can also define any sort of dependent packages that my application is going to depend on in locations where I can get those and you can read more about that on swift.org but you just need to have this file you don't necessarily need to have anything in it for our purposes. So now I'm going to create that. And the next thing I'm going to need is I'm going to create another directory called sources so we're going to create that and I'm going to go into that folder's sources and now in here is where you have your first decision to make. At this point I have the option of having this folder structure created in being build and compiled into either a library or an executable. And the main difference between those two is that an executable can obviously be executed whereas a library has to be imported and used by some other application or some other executable. Now in order to make it one or the other, if you want it to be an executable, then I have to create a file called main.swift. And main.swift, simply the fact that it's named main.swift is gonna make this entire thing into an executable. So let's give this a little bit of code and get rid of the mystery as to why I called this RPS. So I'm going to import Glibc, like that. And then I'm going to create some options here, so I'll say = and this is going to be an array. I'm gonna call this rock, paper and scissors, just like that. So as you can see now, that's hwy I call this RPS, it's a simple little application, little executable that will randomly select rock, paper, scissors, so you could play this with your friends or something like that. So the first thing that I wanna do is I want to seed my randomizer, so I'll say, s random so I'm gonna seed that randomizer and I'm gonna seed it with U Int 32 and I'm going to convert a NS date, which is going to give me the current NS date and I'm gonna say time interval, since 1970. And that's going to give me a seeded value into my random function that I'm going to use here in just a moment. And then I'm going to say print options. And I'm going to call that random function, random, and then I'm going to mod my options.count just like that. So that's all I'm gonna do. This is my simple application. I'm going to import a library, I'm going to create a constant called options with three values in an array, I'm gonna seed my random function, and then I'm gonna use my random function as many times as I need to. So now at this point, so what I would like to do is save this. So let's go ahead and save this into a file. So as you can see main.swift. And now I can comply this on it's own and that would give me something to work off of, but if I want to get a little bit more elegant about this I can jump up a level, and as you can see, I have package.swift, as well as sources. And at this point, I'm going to execute a command here, it's swift build. And that's gonna attempt to create this. And unfortunately, I look to have forgotten an import statement, because I wanna use ns date, but that doesn't exist anywhere just yet. So let's go back into sources. And lets go ahead and modify man dot swift. I also have to import foundations, so I'll say import foundation. So now lets go ahead and save that. So I will come back out here, I will go up a level and once again, I will do swift build, and its going to compile everything for me, its gpnna compile my module RPS. Because that's the folder that I created and it's gonna link to an executable .build/debug/rps. Which means it looks like what it's actually done is it's created a nice directory structure for me using build/debug/rps. So let's take a look at that really quick. So if I come in here you're not gonna see anything. But if I look at this in a little bit more detail, you'll see that I now have a .build folder. So I can go into .build. Within here I have a debug, and within there I could go into debug, and I could see what we have in here. And I have a number of different things that have been created, but as you can see here, I have an executable called rps. So let's jump up a couple level's here. One too far. And now what I can do is I can say I want to execute that. So I wanna go in to .build/debug/rps and that gave me rock, paper, rock, scissors and as you can see now based on the seated value that I used from my random function, I can now get a random value out of my array. So I built a very simple rock, paper, scissors choosing application, if you will. Using the package manager to create a little bit of a folder structure for me and once again, like I said, now I can use this to be able to export some functionality that can imported by other applications, so it's a nice little trick to have. Now on the final lesson I'm gonna show you next, we're gonna build a very simple application that you can actually use on a Linux machine or a whatever have you to help you solve a certain type of a problem.