This workshop will be retired on May 31, 2020.
What's New in Xcode 915:05 with Pasan Premaratne
Since most of day is spent in Xcode, it's worth checking out some of the new features that make writing Swift fun
To wrap up this series, let's talk about some of the changes in Xcode 9. 0:00 These are all wonderful changes that will go a long 0:04 way in improving your day to day work flow. 0:07 With the 9.0 release, Xcode's source editor has been rewritten in Swift. 0:09 The team took this as an opportunity to include some really cool new features, 0:15 including additional customization options, and 0:19 a fully featured markdown editor, but this isn't even the exciting stuff. 0:22 One of the features you will notice immediately is that issues 0:27 have been significantly improved. 0:31 In Xcode 8, issues would often modify the layout of your code if the error or 0:33 issue description was long, and there was a high chance that the error 0:38 described wasn't really the underlying issue. 0:43 So I have opened here an older iOS project in Xcode 9, 0:46 and I'm going to go to a random file. 0:52 So let's go to the Controllers file, go to our Controller, and find a function. 0:55 I'm gonna go ahead and remove the the parentheses for 1:03 the argument list here in the function name. 1:06 When I do that, an error should pop up, 1:10 now this looks much like the older error dialogue. 1:12 But if you click it, 1:15 you can now see that the presentation of this issue is much better. 1:16 Also they've grouped everything so the fix also appears inline. 1:21 And while you can’t see it for 1:25 this issue, multiple fixes will all appear in the same dialogue. 1:26 So let's go ahead and click Fix, and it should solve the problem. 1:30 The new source editor is faster at opening files, and 1:34 faster at scrolling around within these files. 1:37 I don't have a large enough file here, so you'll just have to take my word for it. 1:41 Now, you might have noticed that when I mouse around, 1:45 that certain parts of the code were being highlighted. 1:49 And that's because this new editor has more semantic awareness. 1:52 If you've been playing around with Swift Playgrounds on the iPad, 1:57 you might be familiar with this. 2:00 And brand new in Xcode 9 is a tokenized editing experience. 2:02 So if you hold down Cmd as you move around in your code, 2:06 Xcode highlights the underlying structure. 2:11 For example, it shows you the scope of a function, right here, a method, 2:16 or a computed property. 2:21 You can then click on this structure to bring up a contextual action menu 2:22 that offers options on modifying your code. 2:27 Now this is the money feature, Xcode 9 has a new refactoring engine, and 2:31 this means, finally, that we can refactor Swift code. 2:35 The refactoring engine handles Swift, Objective C, C, and C++. 2:40 But let's take a look at what this looks like in Swift, 2:44 since we've been refactoring by hand for three years now, like cave people. 2:47 The editor takes advantage of this tokenized editing experience to offer 2:52 contextual refactoring when you need it. 2:56 So let's refactor around for a bit, you can see it in action, let's 2:59 navigate to the ItunesAPIClient.swift file, ItunesAPIClient. 3:04 At the top here we have a method, lookupArtist, right there. 3:10 Inside the method, we do some work and 3:17 then create an artist instance after parsing some JSON, right here. 3:19 Here, we're creating the artist instance as part of a guard statement, and 3:25 then assigning the instance to a local constant. 3:29 Now, this is the way I would usually do this, there's nothing wrong with it. 3:32 But let's say we wanted to extract this instant out to a constant to do something 3:35 with it, and then use it. 3:38 So we can select this code, Cmd+click on it, and then select Extract Variable. 3:40 We can now give this variable a name, let's say artist. 3:47 And look at that, it extracts that artist out into its own local constant. 3:51 It then uses that name that we've defined 3:56 back in the guard statement where the code was earlier. 3:59 So let's change this because there's a conflict, we'll say optionalArtist, 4:02 and now we can hit Enter, and everything should work, pretty neat. 4:07 Let's check out a few more things, so 4:11 now let's navigate to the search results controller, where we were. 4:13 In here, we've overwritten prepare for segue, and 4:17 we've provided an implementation. 4:21 So here we have this, if segue that checks their segue, and 4:23 then an implementation inside. 4:27 Well, let's say that we have a ton of different segues 4:29 that we were working with, and this method is starting to get unwieldy. 4:32 So if we hold Cmd on the keyboard and 4:36 highlight the inner if statement, and then click on it, 4:38 the contextual menu offers an option to extract this code out into a method. 4:42 So if we click it, now the code is pulled out into a fileprivate method 4:48 that we can quickly name, so here we'll say showAlbums. 4:53 And just like that, our prepareForSegue method is cleaned up, and 4:59 the logic is all in one place. 5:02 up in viewDidLoad, 5:04 we have some code that sets up a search controller right here. 5:08 Because we're setting up a few things inside viewDidLoad and 5:12 it's confusing, let's extract this to its own method as well. 5:15 Now if we select the code that we want to extract and then Cmd+click. 5:19 You'll notice that there is no option to extract all this out into a method. 5:24 And that's because Xcode can't determine, based on the structure of the code that 5:28 we've selected, that this is necessarily an action that we want to take. 5:32 In such cases, we can right click on any of our selected code, 5:36 go to the Refractor menu, and do the same. 5:41 We can extract this out to a method, and we can say setupSearchController, done. 5:43 Maybe setupSearchController wasn't a great name for 5:51 this method, because it's pretty ambiguous. 5:56 No worries, if you come in, click on the function structure, 6:00 you'll see that you can do a bunch of things, including renaming the function. 6:04 Now when you do this, you see that Xcode has this nice folding animation, and 6:10 reveals all the lines of code where this method is used. 6:14 So we can change the name easily to something like setupController, 6:17 which is a terrible name, but we'll go ahead and do that. 6:21 All right, so let's look at what's going on here, 6:27 you'll see that we have an error, we can click the error menu, and 6:30 we can hopefully fix this issue right there, cool. 6:34 So back to the method we set up, if you look inside this method, one of the things 6:40 we're doing is assigning an object to act as the search results updater. 6:44 This seems like a good object to pass into the function as an argument. 6:49 So we can Cmd+click on the function, and we can select Add Parameter. 6:53 All we have to do now is give it a name, so we'll say, 6:59 withSearchResultsUpdater updater, and 7:01 the type is UISearchResultsUpdating. 7:07 Once we make this change we have an error at the call site. 7:12 We could, again, fix this manually, but the new issues model does all the work for 7:15 us, so all we have to do is click on the issue and hit Fix. 7:19 And now this takes an argument, and here we can say, oops, here we can say self. 7:24 And back in our method, we'll make sure we update this to use the new argument. 7:30 So all in all, we have some really good refactoring tools, 7:37 let's look at one more addition to the editor engine that I really like. 7:39 At the top of the file, let's define a new protocol, 7:44 we will call this protocol SomeProtocol. 7:47 We will say var someProperty of type String is just gettable, 7:50 and a function, SomeFunc, there we go. 7:57 Okay, so now if we specify that searchResultsController conforms to 8:03 SomeProtocol, we'll get an error as we always do, 8:07 because we're missing implementations. 8:11 In Xcode 9, you can now simply click on the issue, and you can add protocol stubs. 8:13 So here it adds all the requirements of the protocol, and leaves placeholders for 8:20 us to insert code. 8:24 If you've ever been in a situation where Xcode complained that you didn't conform 8:27 to the protocol, but you had no idea what was missing, now you have an easy way out. 8:31 So for example, 8:36 if we get rid of the function stub here, the issue will pop back up. 8:37 Now the issue is still present because this is invalid code, 8:41 we don't have an initial value, give it one. 8:45 But now the issue should pop back up, and 8:48 it says, do you want to add protocol stubs? 8:51 Fix, and it adds the missing code, this saves quite a bit of frustration. 8:53 And the nice part, too, is all of this being open sourced, so 8:59 hopefully we're get even more goodies in the long run. 9:03 Let's switch tracks and talk about the compiler real quick. 9:07 Swift 4 is our new language version that we'll all be working with hopefully, but 9:10 the compiler also ships with Swift 3.2. 9:15 Swift 3.2 is not actually a new language version, but a language mode. 9:18 It is how Xcode 9, and therefore the Swift 4 compiler, calls Swift 3 code. 9:24 This means that when you open a Swift 3 project in Xcode 9, 9:29 you can build a project with absolutely no modifications. 9:32 The compiler will run your Swift 3 project in Swift 3.2 mode, 9:37 so you won't be forced to migrate. 9:40 Now it gets better, in Swift 3.2, 9:43 you can use many of the new features that ship with Swift 4. 9:45 So, you can still continue to write Swift 3 code and 9:49 you can use Codable, all the improvements to String, and so on. 9:53 Swift 3.2 also gives you access to the new APIs in the SDK. 9:57 The only thing you don't get by using Swift 3.2 10:01 is Swift related improvements to existing APIs. 10:06 So if we have all these advantages, how is Swift 3.2 different from Swift 4, then? 10:10 Well, with Swift 4, the performance of the language has been improved, and 10:15 your app will benefit from these improvements. 10:19 You'll also get a reduced binary size, which is always a good thing. 10:22 The IOSS decay has some Swift related improvements, and 10:26 in Swift 4 you get access to these as well. 10:29 This is the Swift related improvement I have just mentioned, 10:32 it will not be available to you in 3.2. 10:35 To use 4.0, you will need to migrate your project, but it's not a lot of work, 10:38 particularly if you are involved in a Swift 2 to Swift 3 migration. 10:43 Since a single compiler supports both language modes, 10:48 you can have a single project that can contain both 3.2 and 4.0 targets. 10:52 For example, your app can be in Swift 4, but you can use a package or 10:57 dependency that is still in Swift 3.2, which is huge. 11:02 You will need to recompile your 3.2 targets with the new compiler, but 11:07 that shouldn't hinder you much. 11:10 If you navigate to the build settings of your project, and you search for 11:13 Swift compiler, you'll see that there is a Swift language version setting. 11:19 Now this is an older Swift 3 project that I opened up in Xcode 9, 11:25 so it's automatically in Swift 3.2 mode. 11:28 I don't have to migrate at all, I can just run the project, 11:31 let's switch over to Swift 4 to see what this involves. 11:33 So we'll go to the Edit menu and we'll select Convert > To Current Swift Syntax, 11:37 and notice here that this modal pops up. 11:43 If this modal pops up somewhere during your process, but you don't 11:45 want to convert, you can select Convert Later, and even ask Xcode to remind you. 11:51 Now you'll wanna select the app target to convert, and hit Next. 11:56 When you do this, you'll see that you have to make a decision about how to deal 12:00 with @objc inference in Swift form. 12:05 We've talked about this already, so we're going to minimize inference here. 12:07 As I mentioned earlier, you'll see the tiny print down here that says the built 12:11 product will have a reduced binary size. 12:15 Now I'm just gonna go ahead and Cancel, so you don't have to watch me migrate. 12:19 There are lots of changes under the hood to Xcode 9, and 12:23 while most of them are invisible changes, they should solve a lot 12:26 of problems the Swift community has had over the past few years. 12:30 Xcode's indexers improved in this version, 12:35 which means that open quickly works much faster. 12:37 35 times faster, according to Apple, and 12:40 searching within your project is 50 times faster. 12:43 There are also cool new tools that help with debugging, 12:45 there's a new undefined behavior sanitizer. 12:49 And probably the best thing that no one asked for is the main thread API checker. 12:52 Which informs you if you're trying to update the UI on a thread other than 12:57 the main thread. 13:01 Let's end this series by looking at one final addition to Xcode 9, and 13:02 that is GitHub integration. 13:06 Xcode 9 finally embraces GitHub, and makes it easier to clone projects and 13:08 collaborate, without leaving the IDE. 13:12 From the source control menu up at the top over here, go to the bottom, select clone, 13:15 and a modal should pop up. 13:20 In here, we can search across GitHub for a repo, now granted, 13:23 there's no way to conduct an advanced search. 13:26 But I can go ahead and say, search for the Kickstarter iOS app. 13:29 And I can clone it locally, so this looks like it, let's hit Clone. 13:36 Now we need to select a place to put this, I'm just going to put it on the desktop, 13:42 click Clone, and we'll wait for this to complete. 13:47 Additionally while this clone is in the background, if you go to github.com, 13:50 I have the Kickstarter iOS repo here. 13:55 When the repo contains an Xcode project file, the Clone button should now have 13:57 an Open in Xcode button option as well, so you can directly open this in Xcode. 14:02 So here, we have the Kickstarter project that we just cloned, in the navigator 14:09 area, let's click on the second icon, this is the new source control navigator. 14:14 If you drop down, it lists the various branches, the tags, and 14:20 any remotes, which you can see here, so let's check out the master branch. 14:24 Clicking on the master branch loads the branches' commit history. 14:29 You can drill down and look at any individual commit, 14:34 to see what the pull request changes on the main project. 14:39 You can also select any file, so let me go back here, Let's select this file, 14:44 you can select any of the files that were changed and get a diff of the changes. 14:51 So all really nice stuff, all right within Xcode, so there you have it, 14:57 what's new in Swift 4 and Xcode 9. 15:02
You need to sign up for Treehouse in order to download course files.Sign up