SwiftUI:类型不符合协议“UIViewRepresentable”

Fil*_* Sá 4 xcode swift swiftui

我正在开发一个新的 SwiftUI 应用程序,我正在尝试弄清楚如何使这个 Swift 项目与 SwiftUI 兼容:https : //github.com/suzuki-0000/SKPhotoBrowser

问题是我无法使 UIViewRepresentable 工作。我收到一个错误:

类型“PhotoViewer”不符合协议“UIViewRepresentable”

这是我的代码:

struct PhotoViewer: UIViewRepresentable {

    @Binding var viewerImages:[SKPhoto]
    @Binding var currentPageIndex: Int

    func makeUIView(context: Context) -> SKPhotoBrowser {
        let browser = SKPhotoBrowser(photos: viewerImages)
        browser.initializePageIndex(currentPageIndex)
        browser.delegate = context.coordinator
        return browser
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func updateUIView(_ browser: SKPhotoBrowser, context: Context) {
        browser.photos = viewerImages
        browser.currentPageIndex = currentPageIndex
    }

    class Coordinator: NSObject, SKPhotoBrowserDelegate {

        var control: PhotoViewer

        init(_ control: PhotoViewer) {
            self.control = control
        }

        func didShowPhotoAtIndex(_ browser: PhotoViewer) {
            self.control.currentPageIndex = browser.currentPageIndex
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?

Asp*_*eri 6

SKPhotoBrowser是一个UIViewController子类,所以你需要将它符合UIViewControllerRepresentableUIViewRepresentable

其实差别不大:

struct PhotoViewer: UIViewControllerRepresentable {

    @Binding var viewerImages:[SKPhoto]
    @Binding var currentPageIndex: Int

    func makeUIViewController(context: Context) -> SKPhotoBrowser {
        let browser = SKPhotoBrowser(photos: viewerImages)
        browser.initializePageIndex(currentPageIndex)
        browser.delegate = context.coordinator
        return browser
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func updateUIViewController(_ browser: SKPhotoBrowser, context: Context) {
        browser.photos = viewerImages
        browser.currentPageIndex = currentPageIndex
    }

    class Coordinator: NSObject, SKPhotoBrowserDelegate {

        var control: PhotoViewer

        init(_ control: PhotoViewer) {
            self.control = control
        }

        func didShowPhotoAtIndex(_ browser: PhotoViewer) {
            self.control.currentPageIndex = browser.currentPageIndex
        }

    }
}
Run Code Online (Sandbox Code Playgroud)