SwiftUI.PreviewProvider MainActor isolation warning

330 views Asked by At

I am receiving two (duplicate) warnings about concurrency on a file that is simply a SwiftUI View. This has started to happen after I have added Swift Concurrency on other parts of the codebase.

The full warning is: SwiftUI.PreviewProvider Main actor-isolated static property '_previews' cannot be used to satisfy nonisolated protocol requirement '_previews' declared here

Warning is caused by the CloseButton_Previews below. Here's the full file:

import SwiftUI
import DesignSystem

struct CloseButton: View {
  private enum Constants {
    static let closeIconSize = CGSize(width: 32, height: 32)
  }

  var body: some View {
    HStack {
      IconButton {
        print("Close button was tapped")
      } icon: {
        Icon.closeBig
          .resizable()
          .symbolRenderingMode(.hierarchical)
          .foregroundColor(.gray)
          .frame(
            width: Constants.closeIconSize.width,
            height: Constants.closeIconSize.height,
            alignment: .center
          )
      }
    }
    .frame(
      width: Spacing.minimumTappableLength,
      height: Spacing.minimumTappableLength,
      alignment: .center
    )
  }
}

struct CloseButton_Previews: PreviewProvider {
  static var previews: some View {
    ZStack {
      Color(.systemMint)
      CloseButton()
    }
    .ignoresSafeArea()
  }
}

And the IconButton.swift:

import SwiftUI

public struct IconButton<Icon: View>: View {
  private let action: (() -> Void)
  private let icon: Icon

  public init(action: @escaping (() -> Void), icon: () -> Icon) {
    self.action = action
    self.icon = icon()
  }
   
  public var body: some View {
    Button {
      action()
    } label: {
      icon
    }
    .frame(
      width: Spacing.minimumTappableLength,
      height: Spacing.minimumTappableLength,
      alignment: .center
    )
    .padding()
  }
}

I suspect maybe the problem is somewhere else but somehow the warning is shown on the Preview. Because CloseButton is only used by another SwiftUI View that uses some Swift Concurrency. In itself it doesn't have anything that I can think of that might cause this. Any help would be much appreciated. Thanks!

0

There are 0 answers