Creating a Swift Package in Xcode

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi my name is stuart lynch and in this video i'm going to revisit a topic i covered over a year ago and that's creating and using your own swift packages with swift package manager with swift package manager you can create reusable components of swift content that you can share across different xcode projects this allows you to make changes in one place and then have that code updated everywhere on demand in this video we'll be taking an existing swift ui view and use it to create a swift package what is different this time is that since xcode 5.3 you are now able to include assets in your package so that's what we're going to do in the process i'll show you how you can identify and fix access errors push your package up to a github repository and then test it out by using it in a new project we'll also update the package by adding a new feature and then show how you can update your project and use that package update before i get started please leave a comment below if you enjoy the video give it a thumbs up and subscribe to my channel make sure you ring the bell to be notified of new videos so if this is something you want to learn then keep watching the swift ui view that will be converting to a package is this one that i'm demonstrating here it's a simple view that allows you to present a row of icons or images that when tapped on will change to show a selected rating the view is called ct rating 2 and it requires that you set a maximum rating which corresponds to the number of icons displayed and bind the selected rating to an integer variable so that we can track the current value option clicking on the view struct's name provides you with a summary and description along with an example showing different properties option clicking on one of the arguments brings up more help showing the initializer revealing the other parameters and their initial values but no description of what those parameters are and we'll be fixing that we can add one or more of these other parameters to our view like setting a new width color and rating image like this the images are either sf symbols or an image that is stored in the assets catalog more on this in a second the ratings view is the file of the same name and i'll leave it up to you to explore how it's implemented that's not the point of this video i will point out though that i have included some documentation here for the struct that you saw when we option clicked on the struct in our view if you don't know how to include documentation i have a video for that and i'll share a link for it in the description below another thing to note though is that the range image type is a rating image enum set to the value of dot star the reading image enum lists a number of options that correspond to either an sf symbol where the raw value for the string representation of the enum for an sf symbol has the prefix sf underscore followed by the actual sf simple name and a requirement if you want to add more is that the symbol must also have a corresponding one with the dot fill background if there is no string provided then the view will be looking in the asset catalog for an image corresponding to the case name like these ones shown here and the requirement is that there are two images one with the same name as the case and another one with the dot fill prefix these images are from the google material design collection as great as sf symbols are sometimes you need something a little different back in rating image you see that i have two computed properties that will return an image and depending on whether or not it is an sf symbol or an image asset it'll use the correct image initializer enough for the introduction let's get on with creating our swift package creating a swift package is easy but before i do that i need to decide what i want in my package i'm going to want to have these two files along with the image assets so before i create the package i'm going to create another asset catalog and i'll call it ct ratings 2 images and then i can cut the resources from our asset catalog and paste them into this new one now we can create our swift package from the file menu choose new swift package and give it a name i'll call mine ct rating 2. i like to save mine at the same level but not inside my current project folder what is very important though is that you choose to add it to your project and that you also make sure that the group is the same this will create your package inside your current project and open the swift project manifest file called package.swift first you need to specify what platforms you want to limit the package to and this is an array and i just want to limit mine to ios and because with package manager is only available for ios version 13 or higher we'll need to specify that too if i expand the sources folder there's a placeholder file here i'm going to delete that and i want to replace it with the two files and the asset catalog that i created so let's delete that one now so now we can move those two files and the asset catalog in now the first release of swift package manager supported swift files only so only the dot swift files would be recognized with swift 5.3 we can include resources like this asset catalog but we'll need to adjust the manifest file to be able to use it i'm going to need to know the resource name so let me copy it first to make sure that i don't make any spelling mistakes in the manifest file in the targets array for the target we'll need to add another property called resources which is an array of objects that you want to include and we want to process those files using the dot process method and it requires a string argument which is the path to our asset catalog and one last thing we want to test this package out in our project so we'll need to add it as a framework to our target so we'll select the target and in the frameworks library and embedded content section click on the plus button if you've done things correctly you'll see your framework and all you need to do is add it well our project is broken right now we see that there's an error in our content view as it can't find the ct rating too it's in another module now so we'll have to import it well that's great but it causes more errors and this is because since our project is in another module structs functions and properties of that module are private by default now if you want to learn more about access control levels in swift i have a video for you and i'll leave a link in the description below in the ct ratings 2 file we'll need to make both the struct and the body public this isn't enough however even though the structs have a member-wise initializer that's no use to us outside of the module we'll need to create our own initializer and it has to be publicly accessible so let's do that for each of our properties to initialize a binding like current rating we do it like this and for the remaining properties i'm going to set the default values here in the initializer in the body of the initializer i can set each of the properties to the initializer's arguments to initialize a binding we need to use the underscore character i'm still getting an error on our rating image however and that's because we say that it has to be public and since reading images in the module it is by default private so i'll need to go to that file and make the enum public 2. now this is important the swift package manager creates a static extension on bundle for the package module to access the resource we'll need to specify the bundle.module as the bundle so where we access the image here in these computed properties i'll need to add the bundle property like this now our project builds successfully i can test this out right here in our project both on the canvas and in the simulator let's try one of these images from the framework like the football testing we see that all is good i can now share this package with the world and this requires a remote repository but before i do that i want to be able to provide as much information as i can to anyone that will use this package remember option clicking on the ct rating too shows that help but i'm still not seeing any description for my properties if i return to our ct rating 2 view now that i have an initializer specified i can go to the editor menu and from the structure menu choose add documentation and this creates my properties and some text placeholders to save some time here i'm just going to paste in some that i've already prepared oh and i forgot since we're providing the default values here in the initializer there's no need for me to set them here now when i option click on the property i get help that i provided well now that it's working we can create a github repository where we can have our package stored and if we like make it publicly accessible let's quit the project and locate the ct rating 2 folder it should be at the same level as the ct rating 2 master folder double-click on the package.swiftmanifest file to open xcode we're going to create a repository on github and every good repository has a readme file our swift package has one already created with some placeholder text if you're going to make yours publicly available you should make sure that you update this to provide some helpful information on how to use your framework i'm just going to change this one line here for now but if you want some help on writing markdown which is the format used in a readme file you can check out my video on what i think is the best markdown editor typora i'll leave a link in the notes below when we create our package we had the opportunity to check the box that said create a github repository on your mac i didn't do that so i'm going to create a repository now from the source control menu choose new git repositories this will create and commit all of your swift packages now if you're interested in learning out more about how to use xcode and git for local and remote repositories you can check out my series on my channel if we click on the source control navigator now and expand we get to see the repo file structure if you're going to create a remote repository you will need an account on a remote host like github or bitbucket and you do this in the xcode preferences accounts tab i have mine already set up and i go over this process in the series that i just referenced so back in our project right click on the top folder level and choose new ct ratings to remote you can enter a description like i'm doing here and then decide if you want your repo to be public or private i'm going to keep mine private for now i can always change that later so i can share my package with others now all swift packages must also have an associated number with it so that your project will know if it has the most current version and this is called a tag to tag a branch you can right click on the top folder and choose tag i'm going to set my initial tag to 1.0.0 and create next we need to push our repo and the tag up to github so from the source menu choose push and make sure you select include tags you can quickly get to your uploaded repo by right-clicking again and choose view on github here it is now to import this package into other projects i'll need the repo url so i can just copy it from here let's create a new xcode project to test this out i'll call it testing ct rating 2 and i can just save it anywhere let me stretch it out a bit and change the simulator to an iphone 12 pro to import a swift package you choose swift packages from the file menu and enter the url and click on next it'll try to find your package at that url and if found will show you what version you're getting i'll leave it at the default which is the latest version i'll click on next again and then finish we've successfully added a swift package to our project so let's see how we can use it in our content view we can import ct rating 2. in our content view i'm going to create a v stack with this hello world text and a ct rating 2 view above it i'll accept the default initializer and i'll set the maximum rating to 5 and i see that i need a binding to an integer so i can create a state variable like this one and give it an initial value of zero with that created i can now use that as my binding when i refresh the canvas i see that i have a successful implementation let's try some other options like these perfect what about trying one of these bundled resources i can see that inside the sources folder my asset catalog has been successfully processed let's see if i can use a football image then yes no problem displays properly in the canvas or on the simulator great the final thing i want to cover is updating a package and pushing the update up to our repo and then updating our projects so that it can use this new version now right now i'm limited to the sf symbols and images that i've provided in our package let's exit our test project and return to our master and open it up well since our master is referencing our package folder directly i can modify our file and test it if i want right in our master before committing and pushing up a new version if i open the rating image file i could if i wanted add in more cases for sf symbols and for images and then upload the images to the package image assets folder but what if i want to allow users to use their own images the way i'm going to do this is to create a new case called custom without a specified raw value with the sf underscore prefix this means that our users can provide an image called custom and a corresponding one called custom.fill that our package can use well this is going to require some modifications to the logic in these computed properties too though we're not changing any logic in sf symbols but in this ternary case here where we are selecting an image i'm going to have to check for one more thing and that's whether or not the case raw value is custom if it is we don't want to choose the image from the module bundle we'll want to choose it from the app bundle so we'll leave the bundle option out for our image if the case is a custom and we can use the straight bundle version similarly we'll do the same check for the open image let's exit our main project now and open the swift package the m beside the rating image swift file indicates that it's been modified so we'll need to commit it add a new tag and push it up to the remote repository so i'll commit it with a comment i'll go to the source code navigator and i'll right click and add a new tag and i'll use 1.0.1 and then from the source control menu i'll push it up along with the tag let's open up our test project now from the file menu i can choose swift packages and then update to latest package versions and what this does is it checks all of your packages to see if there's a new version and updates it notice how our version is now showing 1.0.1 so it's been updated which means we can now upload a custom image to our assets folder and use it in the starter resources for this video i've provided two svg images that i'm going to upload to the asset catalog these are vector images and you can see i have a filled and an open version of a graduation cap i'll need to rename these as custom.fill and custom well since they're vector images let's select them both and choose render them both as template images so that they'll be able to change color i can preserve the vector data and set the scale to single scale if you want more information on image assets you can watch my video on that topic i'll leave a link in the notes below well let's test i'm going to change the rating image to dot custom and test yes it's working both in the canvas and in the simulator it's using my own custom image that i've uploaded to my own asset folder great i hope you found this video useful if so please leave a comment below and subscribe to my channel for notifications of new videos
Info
Channel: Stewart Lynch
Views: 2,254
Rating: 5 out of 5
Keywords:
Id: o0fQC7JU-Ts
Channel Id: undefined
Length: 21min 54sec (1314 seconds)
Published: Sun Mar 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.