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!

Xcode tip – rename

When you start developing an app, you don’t often know yet what the real name of your app will be, even you think you know. What do I mean by that?

Well, say you call your app TrainSleeper. You create the new app like this in Xcode and all is fine. Once you create the app in App Store Connect, it might shout at you that someone else owns this name and you cannot use it. You start searching for an app with this name in the store to see it, but it does not exist. This is because you can add an app with a certain name but never publish it. That name stays locked to the person who did this.

Anyway we need a lot of renaming during each development cycle, there is nothing to be ashamed about, it’s a very common task.

For this reason I added a keyboard shortcut to rename.

Give the rename menu function a shortcut.

Go to Settings with CMD + ,

Go to the Key Bindings tab

In filter type rename

In the key column after the rename word press all the keys you want, I selected;

SHIFT + CONTROL + OPTION + COMMAND + R

Why so many keys? Well Xcode has many shortcuts, this avoids conflict with other shortcuts