0:06 Today we'll have a look at the cool new refactoring features of
Xcode 9. Back in the Objective-C days, we actually used AppCode's refactoring a
lot. It changed the way we wrote our code: instead of copying and pasting lines,
we used the refactoring system to extract expressions and methods and to create
variables and properties.
0:52 Now with Xcode 9, we're getting a glimpse of what this way of
working can look like. But the refactoring features are still in beta, so they
don't always work perfectly. Therefore, it's good to have your project under
version control — or there's always the possibility to undo.
1:19 The most important shortcut to remember is Cmd+Shift+A, which brings
up the new Actions menu at the current cursor position:
1:38 When we choose the Rename option, Xcode shows all the places where
ViewController is used or mentioned, including the filename and the storyboard
reference. It even shows that the file header's comment block mentions
ViewController, which is initially grayed out. However, when clicked, it'll
also be replaced.
2:07 Then we can type a new name, like PlayerViewController. When we
build, we find out something went wrong: Xcode updated the file reference in the
project, but it failed to rename the actual file. This shows that the system
isn't completely stable yet.
3:04 Another thing Xcode can do is extract expressions. Let's say we
have the following statement:
3:26 Later we might decide we need to use the subview count in more than
one place, so it'd be good to extract it to a separate expression.
Unfortunately, the shortcut Cmd+Shift+A works only for the current identifier,
view, and not the whole expression. Instead, we have to select the entire
expression, view.subviews.count, and choose the menu option Editor > Refactor
> Extract Expression. This results in the following:
4:19 If we prefer to only use the keyboard to extract the expression, we
could select the expression and press Cmd+Shift+?. This opens the Help menu
search, in which we can simply start typing "extract" and navigate to the
correct menu button. This Help menu shortcut works universally on macOS.
5:01 As an even quicker alternative to using the Help menu, we can
assign a custom shortcut. In Xcode's preferences, under Key Bindings, we can
search for "extract expression" and assign a unique shortcut, say
Cmd+Option+Ctrl+E. This is handy if we use the menu option a lot.
Various Useful Actions
6:17 By pressing Cmd+Shift+A on an if-statement, we can quickly add
else or else if statements:
6:55 There's also more support for working with enums. When we write a
switch statement for an enum, we can wait for the compiler error saying that the
switch must be exhaustive:
8:00 If we add a new case to the enum, we can use the fix-it again.
8:21 You can also use the Cmd+Shift+A shortcut on the switch statement
itself. This gives you more naive options to add a blank case or a default,
without any knowledge of your enum.
9:07 Another useful bit is that you can press the Cmd key and hover over
your code with the mouse. This highlights the scope, which makes the structure
of your code visible. When clicking with the Cmd key pressed, you get the same
Actions menu as with the shortcut Cmd+Shift+A.
9:44 If we select our entire switch statement, we can extract a method
using the Refactor menu:
10:11 Xcode moved the switch statement into a new method,
extractedFunc, and added a call to this method at the line where the statement
used to be. Of course, we can easily rename the method to printState via
10:46 It's interesting how method extraction deals with local variables.
Let's say that instead of printing the state, we want to work with a variable.
We undo the last changes and add a result variable: