Create and Use a Swift Package

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right what's up everybody so in this video i am going to show you how to create your very first and very simple swift package for the swift package manager and then i'm also going to show you how you can incorporate that into an application so there's three parts here first part we're going to create a very simple swift package we're then going to push that up to github where i will then consume that in an application we're just going to create a very simple application ios application just to show that we've successfully pulled down the package and we're making use of it there's a couple of different ways you can create swift packages i'm going to show you the more common way that i think it's used by most people which is where you create a swift package that then gets distributed via github or something like that maybe for other team members or open source now that way of doing it is to independently create a package and then bring it into an application now this will also serve to help explain how you can import other people's switch packages in your application as well so that's the route we're going to take here so i'm going to go to xcode and i'm going to create a new swift package it's going to ask me somewhere to put it i'm just going to put it on my desktop here let's make this window a bit bigger i'm not going to get too much into the details here just going to cover on the swift package the things that you need to know so you can see in the package manifest here it's put in the name of our package we can leave that alone the one that matters over here is under sources my library is where the actual source code is and you can see it creates this very simple struct force now what i'm going to do is i'm going to expose this so i'm going to make it public and i'm not going to bother with the string that they've put in there i'm going to make a public function and i'm going to call it hello to you it's not gonna take any arguments but it is gonna return a string okay and i'm just gonna say return hello to you okay very simple so that's going to be our package so this package is going to have this one function here now when i go to use this of course i'm going to pull it down from somewhere so i'm going to go over to source control here and this part you don't have to do an xcode but it's it's going to be super easy for me to show you here in xcode just going to right click and because of the integration with github it makes it very easy i'm going to create a new github repository remote repository with this library in so i'm just going to say new my library remote i'm going to put it on my github account i'm just going to say this is an example library in the description here and i'm going to make it private because it's of no use to anybody other than this video i'm going to go create and now it's going to go away and it's going to create this wall this repository for me on github and it's going to push it but it hasn't committed my changes yet right so if i commit my changes here with this function so i'm going to say added a basic example function and i'm just going to push it now what i'm doing here this is just you know standard source control i have a video i'll put a link up on the screen for you and in the show notes in case you don't know how to use github with xcode and i'm just going to commit this and push it so that's it that's this part is now complete right that's our swift library now what i'm going to do i'm going to close it here in xcode and i'm just going to pop over to that library i'm going to find my library here that i just created uh wrong one don't want to see my profile we won't see my repositories there we go so there's the my library now what i'm going to do is and you can see here it is right the one that was just pushed from xcode i'm going to click on this drop down i'm just going to copy this url and i'll you'll see why in a minute so can close that i'm now going to go back over to xcode so now we're going to use this new swift package that we've created so i'm going to create a new project i'm just going to make a very simple ios application here nothing fancy i'm just going to call it test app i'm going to leave it as a storyboard of course we want it to be swift i'm gonna go next and store it on the desktop all right there we go okay so at the moment we have this new ios application it's not using our new swift library so let's go ahead and bring that in here if i go up to project and i click on there you're going to see this this tab swift packages if you click on there it's going to be empty right now but this is where we bring in all of those remote packages that we want to use i'm going to click plus and what it's going to do by default it's going to go out and it's going to list the ones that it can find on github right it's going to start by listing my ones you can see it there my library and you can see there's lots of ones from other people as well now you can search by name right i could do my library but here's the thing right when you're creating these there's a hit return there's a good chance that you're going to have the same name for your repository and your pack swift package as many other people as you can see look at all these my libraries right now you know i can look down the list and try and find mine there it's actually right there but the other option is let's just clear that out i copied the url for the git repository to the clipboard so i'm actually going to paste it in there so i can actually say look i know where it is don't bother me don't search for it this is the one i want it's going to go away and try and find that now it's going to come up and say hey which what do you want to do here you want to use like the next major version you want to set some restrictions minor range exact and you can set ranges here well this is my repository and i know that i want to use always use the main branch so i'm going to go ahead and say main here now the reason i say that is because you may have for example it's common to have a develop or a production or a release branch something like that now we're going to cover in a second why this is important i'm just going to go next and it's going to try and resolve and retrieve that swift package for me it's going to take a second to pull it in it's going to go okay i found this library here and i'm going to add it obviously to the target that i've got here yes that's what exactly what i want to do i'm going to go finish now you're going to see it listed over here on the swift package dependencies let's say that you go away you do some work or someone else does some work or something like that on this package well at that point your version is out of date what you can do to check that is if you go to file and then down to swift packages you can tell it to update to the latest package versions right so that's a way to keep that up to date there obviously we know this is up to date right now because i've just done it funny little gotcha here you may be inclined to open this up to explore which you can and you can click on the source code if you click in the editor it's going to give you every impression impression that you can actually edit the code now i'm typing away here on the keyboard and you can see nothing's happening it's kind of a quirk of x code gives you every impression that you can edit the package right here obviously you can't because this is just a dependency it's an external dependency just watch out for that okay so let's go back to our view controller in our application we've now associated this swift package dependency with our app so let's go ahead and use it right so we want to import it first of all so i'm going to say import my library all right now something else you want to look for here if we go over to our target you want to make sure under here under frameworks libraries and embedded content it actually is listing that swift package there and sure enough it's there right so back in our code here we've imported my library now what i can do is i'm just going to create a new instance of it so i'm going to say let my library equal my library and and just instantiate a new instance okay now you're going to see it's got a problem here it's going to say my library initialize in is inaccessible due to internal protection level so i want to point something out here that i feel like swift package manager in xcode should do by default i'm going to go back to the recent i'm going to open up my library again and what the problem is i don't have an init function in here right these are accessible because they're public but it's not initializing like i'm initializing back in my application and you certainly don't have to initialize um but i think it's a good practice to just go ahead and add a public in it right doesn't actually have to do anything but i think it's good practice to have one in there because at some point this library depending on what your usage is you may have a reason to do something there with that so i'm just now going to commit that change all right and i'm going to say i added the inet and i'm going to push that up to the github repository okay and close that now let's go back and open it up in our application you can see we've still got the old version right that new function we just wrote has not come down into our code so i'm going to go file swift packages update the latest package version it's going to take a second here and now when we look you can see it's updated it and it's got the init okay so back on the view controller now what we're going to do here is i'm going to try doing a build and the builds are going to succeed but it's going to say hey look you initialized an instance here but you're not using it it's absolutely correct because what i'm going to do is i'm just going to say print my library dot hello to you so in that library i'm calling that function which as we know is going to return a string right so i'm going to build it make sure everything's okay i'm now going to run it in the simulator so it's going to take a second to spin up here and we're not really interested too much in what happens in the simulator because it's going to print out in the console for us if everything goes according to plan and sure enough if we go over to the console here it's printing the string hello to you which is coming from the function within our library that we included in our application and that is the very basics that you need to know to bring a swift package to create a swift package and bring it down into xcode and use it in your application or use anyone else's in your application hope this has been helpful go to compassrift.com for more videos uh you can also like subscribe here of course leave comments you can also reach out to me on twitter at compile swift i would love to hear your thoughts on this and any requests or suggestions for new videos
Info
Channel: Peter Witham
Views: 2,299
Rating: undefined out of 5
Keywords: xcode, swift, github, source control, swift package manager, swift package, package, manager, tutorial, getting, started, basics, programming, ios development, swift programming, swift dependencies, swift programming language, swift programming tutorial, swift programming basics, app development with swift, xcode 12, xcode 12 tutorial, github projects, github basics
Id: XMaRrJccPv4
Channel Id: undefined
Length: 10min 56sec (656 seconds)
Published: Sun Jan 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.