Speed Up Your Xcode Previews

Dhanajitkapali
4 min readJun 5, 2024

--

Introduction

In large Xcode projects, SwiftUI previews can take a significant amount of time to load. This can be frustrating and slow down your development process. By creating a separate target dedicated to SwiftUI views, you can significantly improve preview performance. This guide will walk you through the steps to create a new lightweight target, add necessary files and dependencies, and use Swift Package Manager (SPM) packages. Follow these steps to streamline your SwiftUI development process.

Table of Contents

1. Creating a New Target
2. Configuring the New Target
3. Adding SwiftUI View Files to the Target
4. Configuring Build Settings
5. Using the New Target for Previews
6. Integrating SPM Packages
7. Cleaning and Building the Project
8. Conclusion

— -

The project initially with only one target.

1. Creating a New Target

1. Open your project in Xcode.
2. Select the project file in the Project Navigator.
3. Click the “+” button at the bottom of the “TARGETS” section.
4. Choose a template, such as “Framework” (a framework target is usually sufficient for previews).
5. Name the new target (e.g., “SwiftUIPreviews”) and configure it as needed.
6. When prompted with “Embed in Application,” choose “None” to create a standalone target.

Click + from Targets section

2. Configuring the New Target

1. Select the new target in the “TARGETS” list.
2. Go to the “General” tab and configure the settings as needed (e.g., deployment info).
3. Ensure to add any necessary dependencies (e.g., frameworks or libraries) that your SwiftUI views require.

3. Adding SwiftUI View Files to the Target

1. In the Project Navigator, select the SwiftUI view files you want to include in the new target.
2. Open the “File Inspector” on the right side (or press `Option` + `Command` + `1`).
3. Under “Target Membership,” check the box next to the new target (e.g., “SwiftUIPreviews”).

4. Configuring Build Settings

1. If your SwiftUI views rely on specific build settings or configurations, ensure these are mirrored in the new target’s build settings.
2. Go to the “Build Settings” or “Build Phases” tab for the new target and adjust settings as necessary.

5. Using the New Target for Previews

1. In your SwiftUI view files, ensure the `@main` attribute is not used or conflicts with your main app target.
2. Open a SwiftUI view file, and in the preview canvas, specify the scheme to use for the preview. Select the new target scheme (e.g., “SwiftUIPreviews”).

6. Integrating SPM Packages

1. Open your project in Xcode.
2. Go to `File` -> `Add Packages…`.
3. Enter the URL of the Swift package repository you want to add, then click “Add Package.”
4. Select the version rules (e.g., “Up to Next Major”) and click “Add Package.”
5. In the dialogue asking which targets to add the package to, ensure that your new framework target (e.g., “SwiftUIPreviews”) is checked, then click “Add Package.”

Add this package in ur newly added target from BuildPhases later on

6. Verify that the SPM package libraries are included:
— Select the project file in the Project Navigator.
— Go to the `Build Phases` tab of your new framework target.
— Under `Link Binary With Libraries`, verify that the SPM package libraries are included.

7. In the SwiftUI view files that are part of the new framework target, import the necessary modules from the SPM packages.

```swift
import SwiftUIPreviews
import SomeSPMPackage
```

7. Cleaning and Building the Project

1. Clean the project by selecting `Product` -> `Clean Build Folder` (or press `Shift` + `Command` + `K`).
2. Build the new framework target to ensure everything is set up correctly and that there are no issues with the package integration.

#### Conclusion

By isolating your SwiftUI views in a separate target, you reduce the overhead caused by unnecessary code and dependencies, which should result in faster preview loading times. This setup allows Xcode to focus only on the essential files required for the SwiftUI views, making the preview process more efficient.

— -

By following these steps, you can streamline your SwiftUI development process and enjoy faster preview loading times.

If you found this guide helpful, please upvote this post! Happy coding!

--

--