1

In a clean demo project I've created a MapView which is UIViewRepresentable and it all works great. It reads a geojson file with a polygon and draws it on the map. When I copy this exact code to my bigger project where i need this functionality I'm getting the following errors:

  1. Type 'MapView' does not confirm to protocol 'UIViewRepresentable'
  2. Value of type 'Context' as no member 'coordinator'

I don't understand why the standalone project compiles perfectly and integrated in my other project it doesn't. iOs versions are set the same

Can someone explain what is wrong ?


import SwiftUI
import MapKit


struct MapView: UIViewRepresentable {  
    var coordinate: CLLocationCoordinate2D
    
    func makeUIView(context: Context) -> MKMapView {
        let map = MKMapView()
        map.mapType = .hybrid
        map.delegate = context.coordinator
        let region = MKCoordinateRegion( center: coordinate, latitudinalMeters: CLLocationDistance(exactly: 5000)!, longitudinalMeters: CLLocationDistance(exactly: 5000)!)
        //map.setRegion(map.regionThatFits(region), animated: true)
        map.centerCoordinate = coordinate
        map.showsCompass = true
        map.showsUserLocation = true
        map.addOverlays(parseGeoJSON())
        
    
        return map
    }
 

    func updateUIView(_ uiView: MKMapView, context: Context) {
        let map = MKMapView()
        map.setNeedsDisplay()
    }
    
    
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    //MARK: Our delegate handler
    final class Coordinator: NSObject, MKMapViewDelegate {
        var control: MapView
        
        init(_ control: MapView) {
            self.control = control
        }
        
        func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer{
            if let polygon = overlay as? MKPolygon {
                
                let renderer = MKPolygonRenderer(polygon: overlay as! MKPolygon)
                renderer.fillColor = UIColor(red: 57 / 255, green: 255/255, blue: 20/255, alpha: 0.5)
                //renderer.fillColor = UIColor(red: 192 / 255, green: 45/255, blue: 233/255, alpha: 0.5)
                renderer.strokeColor = UIColor.black
                renderer.lineWidth = 2
                return renderer
                
 
            }
            return MKOverlayRenderer(overlay: overlay)
        }
 
        
    }
    func parseGeoJSON() -> [MKOverlay]{
        guard let url = Bundle.main.url(forResource: "zone", withExtension: "json") else {
            fatalError("NOT FOUND")
        }
        var geoJson = [MKGeoJSONObject]()
        do{
            let data = try Data(contentsOf: url)
            geoJson = try MKGeoJSONDecoder().decode(data)
        } catch {

        }
        var overlays = [MKOverlay]()
        for item in geoJson{
            if let feature = item as? MKGeoJSONFeature {
                for geo in feature.geometry{
                    if let polygon = geo as? MKPolygon{
                        overlays.append(polygon)
                    }
                }
            }
        }
        return overlays
    }
}
 


struct ContentView: View {

    var body: some View {
        ZStack(alignment: .top) {
            MapView(coordinate: CLLocationCoordinate2D(latitude:34.2226,longitude:-82.4592))
        }
        .ignoresSafeArea()
        .navigationBarHidden(true)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
            ContentView()
    }
}

Peter Van de Put
  • 878
  • 10
  • 26
  • Does this answer your question? [SwiftUI: Type does not conform to protocol 'UIViewRepresentable' // My Code](https://stackoverflow.com/questions/75176496/swiftui-type-does-not-conform-to-protocol-uiviewrepresentable-my-code) – lorem ipsum May 22 '23 at 18:55

2 Answers2

0

Definitely it conflicts with built-in MapView - just name it differently. The reason might be not clear - just which one is detected by compiler in cache (it is awful in today Xcode). IMO it is bad practice to give same names as system has, independently of namespaces - rarely who uses prefixes. Just in case.

Asperi
  • 228,894
  • 20
  • 464
  • 690
0

In my case I had a class called "Context" that conflicts with Context of Swift UI, I just changed the name to "MyContext" and it works for me.