不符合协议 ‘UIViewControllerRepresentable’

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

Does not conform to protocol 'UIViewControllerRepresentable'

问题

I'm completing the online apple Xcode Tutorial. At the UIKit tutorial portion, I'm repeatedly getting the same error:

"Type 'PageViewController' does not conform to protocol 'UIViewControllerRepresentable'"

That portion of the tutorial is a few lines which are copied and pasted:

struct PageViewController<Page: View>: UIViewControllerRepresentable {
    var pages: [Page]
}

How can I address this does not conform to protocol?

There are a few Does not conform to protocol errors posts online, each seemingly addressing the same error differently. Clicking fix the error adds this line to the file:

"typealias UIViewControllerType = <#type#>"

Which doesn't address the issue since the same error occurs.

英文:

I'm completing the online apple Xcode Tutorial. At the UIKit tutorial portion, I'm repeatedly getting the same error:

 &quot;Type &#39;PageViewController&lt;Page&gt;&#39; does not conform to protocol &#39;UIViewControllerRepresentable&#39;&quot;

That portion of the tutorial is a few lines which are copied and pasted:

 struct PageViewController&lt;Page: View&gt;: UIViewControllerRepresentable {
      var pages: [Page]
  }

How can I address this does not conform to protocol.

There are a few Does not conform to protocol errors posts online, each seemingly addressing the same error differently. Clicking fix the error adds this line to the file:

&quot;typealias UIViewControllerType = &lt;#type#&gt;&quot;

Which doesn't address the issue since the same error occurs.

答案1

得分: 1

你应该也要实现存根,就像这样:

struct PageViewController<Page: View>: UIViewControllerRepresentable {
    typealias UIViewControllerType = UIViewController // <- 选择正确的控制器

    func makeUIViewController(context: Context) -> UIViewControllerType {
        // 根据需要实现
    }

    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        // 根据需要实现
    }

    var pages: [Page]
}
英文:

You should implement the stubs too, like:

struct PageViewController&lt;Page: View&gt;: UIViewControllerRepresentable {
    typealias UIViewControllerType = UIViewController // &lt;- Choose the correct controller

    func makeUIViewController(context: Context) -&gt; UIViewControllerType {
        // Implement As needed
    }

    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        // Implement As needed
    }

    var pages: [Page]
}

huangapple
  • 本文由 发表于 2023年5月7日 17:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193067.html
匿名

发表评论

匿名网友

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

确定