Type 'TextView' does not conform to protocol 'UIViewRepresentable'
Asked Answered
C

2

5

I started to experiment SwiftUI stuff in a new clean project.

Inside this new project, created a struct that conforms to UIViewRepresentable. The idea is to embed a UIKit view inside a SwiftUI view. Everything works fine.

import SwiftUI

struct ExempleFromSwitfUIBasedProject: View {
    
    @State var text = "Hello World"
    
    var body: some View {
        VStack {
            TextView(text: $text)
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 44.0)
        }
    }
}

struct TextView: UIViewRepresentable {
    @Binding var text: String

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

    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.text = text
        uiView.backgroundColor = UIColor.red
    }
}

struct ExempleFromSwitfUIBasedProject_Previews: PreviewProvider {
    static var previews: some View {
        ExempleFromSwitfUIBasedProject()
    }
}

Same code visible in my IDE to show no error is triggered

enter image description here

Issue came while I try to embed the EXACT SAME CODE (copy/paste of the file) in my legacy project.

This leads me to the following error :

Type 'TextView' does not conform to protocol 'UIViewRepresentable'

import SwiftUI

struct ExempleFromLegacyBasedProject: View {
    
    @State var text = "Hello World"
    
    var body: some View {
        VStack {
            TextView(text: $text)
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 44.0)
        }
    }
}

struct TextView: UIViewRepresentable {
    @Binding var text: String

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

    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.text = text
        uiView.backgroundColor = UIColor.red
    }
}

struct ExempleFromSwitfUIBasedProject_Previews: PreviewProvider {
    static var previews: some View {
        ExempleFromLegacyBasedProject()
    }
}

Same code visible in my IDE to show the triggered error

enter image description here

To summarise: In a clean project, I use the 'UIViewRepresentable' protocole without any trouble. In a legacy project created few years ago, i just copy past the same file, which raise me a compile error.

Both projects have a min deployment target >= iOS 13.

Thanks for your help

Chattanooga answered 11/12, 2020 at 10:52 Comment(1)
You need to edit your question to include all relevant code as text, using proper code formatting - and not as a screenshot -, in the form of a minimal reproducible example in order to make the question on-topic.Ejective
C
26

After struggling few hours, I find out what was wrong.

My legacy project used a third party library which referenced a Class named "Context" as well.

In my case, instead of referencing the correct Context which should be from the SwiftUI framework.

typealias Context = UIViewRepresentableContext

Xcode was referring to a Context object related to a third party class rather than the SwiftUI's.

A way to get rid of this is issue for me was to replace "Context" keyword with

UIViewRepresentableContext<TextView>

Below, you can find the full code updated:

import SwiftUI

struct ExempleFromLegacyBasedProject: View {
    
    @State var text = "Hello World"
    
    var body: some View {
        VStack {
            TextView(text: $text)
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 44.0)
        }
    }
}

struct TextView: UIViewRepresentable {
    @Binding var text: String

    func makeUIView(context: UIViewRepresentableContext<TextView>) -> UITextView {
        return UITextView()
    }

    func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<TextView>) {
        uiView.text = text
        uiView.backgroundColor = UIColor.red
    }
}

struct ExempleFromSwitfUIBasedProject_Previews: PreviewProvider {
    static var previews: some View {
        ExempleFromLegacyBasedProject()
    }
}

Here under, you can see a screenshot from the IDE without any error:

enter image description here

Btw, a hint should lead me to the issue:

  • The Context keyword is purple when refers to the SwiftUI lib.
  • The Context keyword is green when it's referenced to my own Class.
Chattanooga answered 11/12, 2020 at 15:49 Comment(1)
OMG THIS ^^^^^ I spent almost a day trying to figure out why it is not compiling, and it was this!!Vitek
C
0

On iOS 16

typealias UIViewType = UITextView
Closet answered 17/9, 2022 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.