在范围内找不到类型“UIViewRepresentable”

Sco*_*des 1 swiftui uiviewrepresentable

import SwiftUI
#if canImport(WebKit)
import WebKit
#endif

struct WebView: UIViewRepresentable {

    var url: URL

    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
    }

    func updateUIView(_ webView: WKWebView, context: Context) {
        let request = URLRequest(url: url)
        webView.load(request)
    }
}

Run Code Online (Sandbox Code Playgroud)

我将我的应用程序创建为多平台应用程序。我添加了这个视图。但是我遇到了一些我无法弄清楚的错误。

Cannot find type 'UIViewRepresentable' in scope Cannot find type 'Context' in scope

我清除了构建文件夹。我的最低部署目标如下:

在此输入图像描述

我希望它针对以下方案运行:在此输入图像描述 在此输入图像描述

lor*_*sum 9

UIViewRepresentable
Run Code Online (Sandbox Code Playgroud)

适用于 iOS 和 MacCatalyst

NSViewRepresentable
Run Code Online (Sandbox Code Playgroud)

适用于 macOS

确保您正在检查目标,您可能正在为 macOS 构建。

#if os(iOS)
     UIViewRepresentable code

#endif
Run Code Online (Sandbox Code Playgroud)