Xcode SwiftUI Preview Tips

Loved by some, hated by many developers — is the SwiftUI preview. I was also in the latter camp to be honest, until recently.

Here are some things you can do to enjoy your SwiftUI previews again.

When you get an error, tap on the error and read it carefully, often it gives a hint. For example, you have been changing code left and right, then you went to this certain view, and you activated the preview pane with Option+Command+Enter — and getting an error.

Make sure before activating a preview canvas, that your code builds and if not, then fix the error and try again.

If you have done this and you still get an error, make sure you don’t have another tab open with a preview active for another target, close those and try again.

If that also fails you can try to clear the preview cache with this terminal command, I made an alias in .zprofile called simprevdel.

Compiler flag PREVIEW

You can add the PREVIEW compiler flag to use it as a compiler directive to load preview mock data.

Don’t forget to inject the EnvironmentObject model in the Preview View.

Enjoy your previews!

Automate your Xcode workflow / Dev tools with keyboard shortcuts

Hardware

As developers, we start and switch a lot between common developer applications each day. You can buy some external hardware keyboards to achieve this, like for example with these USB Mini 3-Key Keypad, or the HUION Keydial, 9-Keys USB Mini Keypad, Programming Macro Pad. They work fine but you must always pack them in your bag when going to your workspace, but make sure before purchasing them, that they have macOS software drivers.

macOS Shortcuts

Personally I have the Huion, and use it for general Xcode keyboard shortcuts — like show / hide panes, delete current line, Run, Stop, etc, and to save keys I use macOS Shortcuts to launch applications like Xcode.

To achieve this do the following:

  1. open Shortcuts
  2. tap on the + in the title bar
  3. In the categories tab on the right, select Open App, and drag it on the canvas
  4. Click on App, to select Xcode
  5. Click on the info circle top right > Add keyboard shortcut > Control+Option+Command+x
  6. If you see on the canvas, Receive input from, deselect all
  7. Tap on the default name in the title: Open App, to change it to Open Xcode, and you can optionally assign an icon, but this is not really needed.
  8. Now comes the confusing part, there is no save, you just close this window with the red x
  9. Close Shortcuts
  10. Test by tapping Control+Option+Command+x

Other Dev tools

Now you can repeat this process for your most common developer tools like SourceTree, Interactful, Slack, enjoy!