You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI.PreviewProvider主Actor隔离重复警告问题求助

SwiftUI预览出现Main Actor隔离并发警告问题

在一个仅包含SwiftUI View的文件中,出现了两条重复的并发警告,这是在代码库其他部分引入Swift Concurrency后才开始出现的。

完整警告内容:

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

该警告由CloseButton_Previews触发,以下是完整的CloseButton.swift代码:

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()
  }
}

以下是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()
  }
}

我怀疑问题根源不在当前文件,但警告却指向预览代码。CloseButton本身没有任何并发相关代码,只是被一个使用了Swift Concurrency的SwiftUI View调用,希望得到解决帮助。


内容的提问来源于stack exchange,提问作者Pink

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 15:05:20