SwiftUI.PreviewProvider MainActor isolation warning
Asked Answered
M

0

9

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!

Moir answered 27/12, 2022 at 14:12 Comment(1)
Did you manage to find the solution?Vries

© 2022 - 2024 — McMap. All rights reserved.