Chapter 2: how to quickly put each of the method parameters on a separate line

I’m wondering if there is a keyboard shortcut to put every parameter of a method on a separate line.

For example from the IBAction method in chapter 2:

I start entering let alert = UIAlertController

As soon as I type ‘(’ I get a drop-down box with ‘different forms’ of the UIAlertController method:

Screenshot 2021-07-10 at 17.36.20

I hit 3x the down arrow, to select the last one of the list, with 3 method parameters:

Screenshot 2021-07-10 at 17.36.25

Then I hit enter, end I get this method template, with the first parameter already in blue, ready to enter the String:

Screenshot 2021-07-10 at 17.24.18

I enter “Hello World” for the title parameter (on the ‘string placeholder’, so it becomes this:

Screenshot 2021-07-10 at 17.37.12

Then I hit tab to move to the 2nd parameter, and start typing
Then I hit tab again to the last parameter.
The end result looks like this:

Screenshot 2021-07-10 at 17.29.56

Now I want to put every parameter on a separate line, just as in the screenshot in the book, so it becomes this:

Screenshot 2021-07-10 at 17.30.55

Now the only way that I can find out to do this, is to take the mouse, click before every parameter, and hit enter.

Question: I don’t want to touch my mouse while coding, so I’m wondering if there is a shortcut to do this, i.e. while completing the 3 parameters.

Short answer: No :slight_smile:

Long answer: Not that I know of where you can do so with Xcode. You might be able to use a code formatter to do so post-save and/or when you invoke the formatter. You might also be able to find a plugin which does this for you but Xcode by default will not do so.

The reason that the book puts every parameter on a new line is so that it’s easy to read and to avoid confusion when trying to type in code. Personally, I prefer to have everything on line because it seems neater (and easier to parse) for me :slight_smile:

1 Like

RW style used to put as much as would fit into each line — about 70-something characters. Then 2-space indentation for following lines. But now that we use SwiftLint, we pretty much have to do one parameter per line, if it doesn’t fit in one line.

1 Like