DEV Community

loading...

Three Handy Xcode and Swift Tips

iamstephenbrown profile image Stephen Brown ・2 min read

Xcode is a great tool for developing iOS applications and it is packed with a huge amount of features. The problem is that they are not always easy to find and have caused me frustration in the past.

Below I will highlight a few of the, I think, lesser-known features and shortcuts that make your life easier in Xcode.

Generate Memberwise Initialisiers

Swift structs come fully loaded with automatic init generation, which can save you a lot of time. Classes are not so lucky.

Screen Shot 2020-12-02 at 9.33.11 AM

We have all spent far too long writing our own init for a class, but no longer! Xcode offers a handy feature that generates them for us.

Screen Shot 2020-12-02 at 9.33.43 AM

By right-clicking on the class name and opening the refactor menu, we are presented with the "Generate Memberwise Initializer" option. This does what it says on the tin and generates an init for us.

Screen Shot 2020-12-02 at 9.34.08 AM

Multi Cursor Editing

Most IDEs and text editors have powerful multi cursor functionality, allowing you to select multiple lines and edit them at the same time.

It is less well known that Xcode offers this functionality too! It is generally a little less powerful than most editors offer, but it gets the job done if used smartly.

Screen Shot 2020-12-02 at 9.45.46 AM

A great use for this feature is when working with the Codable protocol. We all love using camel case when writing Swift, but the APIs we connect to seem to always work in snake case. Setting up the Coding Keys for Codable to correctly read the returned JSON can be a pain, especially for more complex responses.

Using the multi cursor select we can copy the class variables into the CodingKeys struct and use control + shift + down (or control + shift + click) to get multiple cursors and fill them out.

Quick Open

A really handy shortcut for finding files and classes in Xcode is using quick open, which can be triggered by using command + shift + O.

Open the interface and start typing the name of the class, variable, or file that you want and tap enter to open it.

Screen Shot 2020-12-02 at 10.07.01 AM

Discussion

pic
Editor guide
Collapse
sugersuger profile image
Sei Sato

So useful! Thanks for sharing it.