iosswiftswiftuiuiviewrepresentable

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


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


Solution

  • 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: