‘type’ 的值为 ‘some UIView’ 没有成员 ‘scrollView’。

huangapple go评论61阅读模式
英文:

Value of type 'some UIView' has no member 'scrollView'

问题

我正在尝试在我的Swift UI应用程序中加载YouTube视频。

我正在遵循的教程编写了以下代码:

uiView.scrollView.isScrollEnabled = false
uiView.load(URLRequest(url: YouTubeURL))

在我的情况下,我收到以下错误消息:
类型'some UIView'没有成员'scrollView'

如果有帮助,这是整个结构:

struct Video: UIViewRepresentable {
    let videoId: String?
    
    func makeUIView(context: Context) -> some UIView {
        return WKWebView()
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) {
        guard let ytId = videoId else { return }
        let ytUrl = URL(string: baseUrl + ytId)
        
        uiView.scrollView
        uiView.load
    }
}

有什么办法修复这个问题吗?

英文:

I'm trying to load a youtube video in my app in swift ui

Tutorial I'm following writes the following code

uiView.scrollView.isScrollEnabled = false
uiView. load(URLRequest(url: YouTubeURL))

In my case, I get the following error
Value of type 'some UIView' has no member 'scrollView'

That's the whole struct if it helps

struct Video: UIViewRepresentable {
    let videoId: String?
    
    func makeUIView(context: Context) -> some UIView {
        return WKWebView()
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) {
        guard let ytId = videoId else { return }
        let ytUrl = URL(string: baseUrl + ytId)
        
        uiView.scrollView
        uiView.load
        
    }
}

Any idea how to fix that?

答案1

得分: 1

你可能根据实现 UIViewRepresentable 协议来接受了这一行的自动完成:

func makeUIView(context: Context) -> some UIView {

这意味着结果是“某种类型的 UIView,但调用者不知道具体是哪种类型。” 作为这个协议的一部分,它还将 UIViewType 设置为 some UIView。然后你不能在其上使用 WKWebView 特定的属性。你很可能本意是要写成:

func makeUIView(context: Context) -> WKWebView {

你可能还想要更改这一行,明确使用 WKWebView,以便更加清晰:

func updateUIView(_ uiView: WKWebView, context: Context) {
英文:

You probably accepted the auto-completion for this line based on implementing the UIViewRepresentable protocol:

func makeUIView(context: Context) -> some UIView {

This says that the result is "some kind of UIView, but the caller doesn't know what kind." As part of this protocol, it also sets UIViewType to be some UIView. You can't then use WKWebView-specific properties on that. What you very likely meant to write here was:

func makeUIView(context: Context) -> WKWebView {

You likely also want to change this line to use WKWebView explicitly, in order to be a bit clearer:

func updateUIView(_ uiView: WKWebView, context: Context) {

huangapple
  • 本文由 发表于 2023年7月11日 04:48:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定