This workshop will be retired on May 31, 2020.
In this short video, we'll tell you all you need to know to get started using Carthage in your iOS projects!
Swift 3 Update
If you are following along using Xcode 8 and Swift 3 then please make sure to use a new version of
SwiftRandom. This is what you will add to your
github "thellimist/SwiftRandom" == 1.0.0
[SOUND] Hi, my name's Gabe, and I'm an iOS developer. 0:00 In this short workshop, we're gonna talk about dependency managers, why they're so 0:07 useful, and then try our hand at Carthage, a widely used dependency manager. 0:11 By the way, this is just the type of workshop that you might wanna 0:15 follow along with step-by-step. 0:18 So we've linked a text version below, complete with screenshots and 0:20 lots of instructions. 0:24 You may find that useful in lieu of or in addition to these videos. 0:25 So when working in iOS, there are plenty of amazing third party libraries that we 0:29 can use to save us time and energy as we develop our apps. 0:34 Third party libraries can provide useful methods or templates and 0:37 can assist in networking, parsing data, interfacing with core data, 0:40 fetching web images, and many other common tasks. 0:44 Once integrated, 0:47 our project becomes dependent upon them, which is why we call them dependencies. 0:48 Now if you've ever integrated libraries into an app manually and 0:53 then tried to keep them up to date as the OS, your code, and the libraries 0:56 themselves all change over time, you know that can quickly become a beast to manage. 1:01 Well, dependency managers to the rescue. 1:06 Most dependency managers will download your desired 1:09 third party libraries in the relevant versions, prepare them for 1:11 your project, and obtain any other required dependencies. 1:15 There are also plenty of useful libraries that can be found with 1:19 a little searching on the Internet. 1:22 Many of these are kept on GitHub. 1:24 For example, AlamoFire is a fantastic library for networking. 1:25 ObjectMapper helps to transfer model objects to and from JSON, and 1:29 SugarRecord is great for leveraging core data in iOS applications. 1:34 For our grand adventure into dependency managers, 1:38 we'll be using a library of functions and methods known as Swift Random, 1:41 the dependency manager Carthage, and the Treehouse Xcode project, FunFacts. 1:45 After using Carthage to download and prepare the Swift Random library, 1:50 we'll be using the random color method from Swift Random 1:54 in place of the current random color method that we wrote. 1:57 Though, before we begin, let's just take a quick glance at the road ahead. 2:01 First, we'll install and verify Carthage on our machine. 2:05 You'll only have to do that once, of course, 2:09 not every time you wanna use Carthage to manage a project. 2:10 Next, we'll add a Cartfile to our FunFacts project, 2:14 which will tell Carthage which dependencies we care about. 2:17 After that, we'll run Carthage and take care of some housekeeping, 2:20 like linking frameworks and stripping architectures. 2:23 And then finally, we'll put our library to use in our FunFacts app. 2:25 If you enjoy this video and wanna learn about other dependency managers, 2:29 like Cocoa Pods, I'd encourage you to check the link below. 2:33 I'd also encourage you to keep this option of third party libraries 2:36 front of mind as you flesh out project ideas and work through your existing apps. 2:39 Finding the right libraries can drastically cut down your production time 2:44 and bring all sorts of wonderful functionality into apps 2:47 that might not otherwise warrant custom solutions. 2:51 If you find libraries you love, share them with your favorite developers, and 2:53 don't forget to thank the authors. 2:57 Who knows, maybe one day, you'll be that author. 2:58 So the Carthage installer can be downloaded from this page on GitHub, 3:02 and I've linked that below. 3:06 We can scroll down here and download it. 3:07 We'll go ahead and open it. 3:13 You may be greeted with a little security warning before you do that. 3:14 If you do, you just wanna head over to the preferences on your system and 3:17 then allow installation of Carthage. 3:21 Okay, looks like our installation was successful. 3:29 We're still gonna verify this in a second. 3:31 Now, to verify we installed Carthage correctly, we're gonna use Terminal. 3:34 And by the way, 3:38 working from the command line is something all developers should be familiar with. 3:39 In fact, you might wanna just drag Terminal right into your dock 3:43 because you'll probably use it again and again. 3:46 If you'd like to know more about Terminal, 3:48 we have a fantastic course detailing Terminal basics. 3:50 And if you don't have time for that, there is a cheat sheet I've 3:53 also linked below that shows some of the common Terminal commands, and 3:56 that can be a great resource too. 3:59 So, we'll open Terminal by heading to Applications > Utilities, 4:01 And Terminal, and I'm gonna drop it in my dock as well. 4:10 Okay, now inside Terminal, we're gonna type in, carthage version. 4:14 And there we see version 0.16.2. 4:23 Now it might be different for you if you're installing this later than I am, so 4:27 don't let that worry you. 4:31 If no version is displayed, however, you're gonna need to stop, go back, 4:33 and repeat the steps we already went over, and make sure you do it correctly. 4:36 So here we are inside our FunFacts app, and of course, that's linked below. 4:42 If you haven't watched the course where we build this app, 4:46 that too is linked down under. 4:48 And if we run our app, we're gonna see how, in the sim, 4:50 we show facts, and each time, we're displaying a new color. 4:55 Now that's great and all, but we came here for bigger and better things. 5:01 So let's install our Swift Random library with the help of Carthage. 5:04 First we're gonna need to head back to Terminal, and now, 5:08 we're gonna wanna navigate to the folder containing our FunFacts project. 5:12 Now we're gonna do that by typing cd, and 5:18 then we're gonna drag in the folder that contains our FunFacts projects. 5:21 So, we're gonna drag this right in there. 5:27 And that threw an error there because there's no space between the cd, 5:36 I just need to hit cd, then space, then we'll drag that in. 5:40 And now, to make sure we're in the right place, 5:45 we're gonna use the list command, ls. 5:47 And it'll show FunFacts and FunFacts proj, which is right here, so 5:50 we know that we're in the right place. 5:55 Now since we know we're in the right directory, we're gonna type, 5:58 touch Cartfile. 6:02 And that's gonna create a Cartfile for us. 6:05 Next we're gonna wanna open up that Cartfile, 6:08 and we can do that by typing, open -a Xcode Cartfile. 6:14 And here we go, we see our empty Cartfile. 6:22 Now because the Cartfile is the link between Carthage and our project, 6:25 it's crucial to ensure that every line is written absolutely perfectly. 6:28 When going through this tutorial, 6:32 it's most important that you understand the following concept about Carthage. 6:34 The Cartfile provides the instructions 6:37 that tells Carthage where to find the desired library, 6:40 the name of the library, and what version of the library to download. 6:43 So you're gonna wanna type in exactly, 6:47 github "thellimist/SwiftRandom", 6:55 then close the quotes, 7:03 == 0.1.7. 7:07 We're gonna save our changes, and then we're gonna head back to Terminal, 7:10 and we're gonna run Carthage by typing, carthage update --platform iOS. 7:17 Carthage will now read the Cartfile, retrieve the appropriate library, and 7:29 build the library into a framework. 7:33 Now, if your Terminal looks a little different from how mine looks, it's okay, 7:35 don't panic. 7:39 Just make sure that the Terminal displays Building scheme "SwiftRandom-iOS", 7:40 like it does right here. 7:46 It's also possible that you'll get a warning about bad credentials. 7:48 That warning probably is indicating that you're not logged in to GitHub, and 7:52 you can ignore that if you want to. 7:55 The Swift Random library is still gonna be downloaded and built for you. 7:57 So once this is finished updating, you'll have the Swift Random framework for 8:01 use in your project. 8:05 If FunFacts isn't already open, open it. 8:07 But for us, it is. 8:10 We'll just hop over here. 8:11 I'm gonna stop it from running, and we're gonna wanna link our frameworks now. 8:12 To do that, we're gonna click on FunFacts. 8:18 We'll go to the General tab, and then we're gonna scroll down to the bottom, 8:21 where we'll see Linked Frameworks and Libraries. 8:25 We'll just stop that from running. 8:31 I'll reduce the size of this window a little bit, and 8:33 that's because we're gonna wanna open up a Finder window. 8:35 And inside the FunFacts directory, 8:38 we should see Carthage, Build, iOS. 8:42 And inside, we see this SwiftRandom.framework. 8:47 You're just gonna drag that right into here. 8:50 And it should show up like that. 8:54 Sometimes it doesn't stick the first time you drag it. 8:56 So you wanna make sure it's there. 8:58 And voila, we have added the framework, and we're finished with this step. 9:01 Now you might think you're totally finished, 9:06 and you might be if there weren't some bugs involved with universal 9:08 frameworks being submitted to the App Store. 9:13 So we're gonna strip out an architecture. 9:15 And the way that we do that is we're gonna go to Build Phases, and 9:18 we're gonna click this plus sign right here, and select New Run Script Phase. 9:21 After that, we're gonna open up that enclosure triangle. 9:29 And right here, where it says type a script, we're gonna delete that out, 9:34 and instead, we're gonna write in the following path. 9:39 We're gonna click this plus as well, and 9:52 then we're gonna add, This into the field. 9:56 Now, of course, you've gotta be real sure that you're typing this stuff exactly 10:11 right, or it's not gonna work for you. 10:14 So with those now in our project, our new library should be ready to use. 10:16 We're gonna build and run it again, just to be sure. 10:23 Okay, and now, let's actually put our library to use. 10:27 We'll go FunFacts, and we'll head into ViewController, and 10:32 we're gonna scroll down to the bottom. 10:36 And here, we're gonna comment out this line, 10:38 where we set our randomColor, and we're gonna type in a new line. 10:41 So we can use our new random function to set the color. 10:48 Okay, we're getting an error. 11:00 Who knows why? 11:03 That's right, we never imported our library up top. 11:04 So let's not forget to do that, import, and our library's called SwiftRandom, 11:09 there we go, it knew just what we were talking about. 11:13 All right, let's run and build it again. 11:16 All right, here we go, a whole new array of colors, very cool, 11:20 and our code is just humming along beautifully with our new library. 11:25 All right, we'll stop that for now. 11:31 The last thing we're gonna do before we leave is check out how we would 11:33 update the library, and you may in the future wanna do this yourself. 11:36 This is one of the best parts of having a dependency manager, 11:40 is that it keeps these things in sync and up to date for you. 11:44 Now to do that, we would head over to our Cartfile again. 11:48 And let's say that we wanted to change it from = 0.1.7 11:53 to greater than or equal to 0.1.7. 12:00 We could do that, and then we'd head back over to Terminal, and again, we'd hit 12:05 carthage update --platform iOS, and I could do that just by doing the up arrow. 12:10 It'll take my last command and copy it in there. 12:15 And there we see it would rebuild it with the new version. 12:18 Pretty cool, yeah? 12:23 Why don't we head back to the studio, and we'll wrap this up? 12:24 All right, now if that random color method didn't knock your socks off, 12:28 don't feel deflated. 12:32 What we've really learned here isn't how to implement one method from one library, 12:33 but rather, how to gain access to all the amazing functionality, 12:37 in all the amazing libraries out there today, as well as those yet to come. 12:40 Perhaps more importantly, using a dependency manager like Carthage 12:44 will help keep your apps and libraries in sync, so you can leverage those libraries 12:48 to fearlessly build ever more full featured experiences for your users. 12:52 Thanks for dropping in, and if I don't see you in the future, 12:56 then I'll see you in the pasture, take it easy. 12:59
You need to sign up for Treehouse in order to download course files.Sign up