Home > Mobile >  Error message in SwiftUI: Type '() -> ()' cannot conform to 'ShapeStyle'
Error message in SwiftUI: Type '() -> ()' cannot conform to 'ShapeStyle'

Time:02-23

I wrote an extension for the instructions inside the application, after writing an error came out:

Type '() -> ()' cannot conform to 'ShapeStyle'

Here is the extension itself and here is the error:

extension View{
    //MARK: - Custom Spotlight Modefier
    func spotlight(enabled: Bool, title: String = "")->some View{ 
        return self  //Error Message: Type '() -> ()' cannot conform to 'ShapeStyle'
            .overlay{
                if enabled{
                    //To Get the Current Content Size
                    GeometryReader{proxy in
                        let rect = proxy.frame(in: .global)
                        
                        SpotlightView(rect: rect, content: title){
                            self
                        }
                    }
                }
            }
    }
    
    //MARK: - Screen Bounds
    func screenBound()->CGRect{ }

    //MARK: - Root Controller
    func rootController()->UIViewController{ }
}

The structure that is needed to display instructions

struct SpotlightView<Content: View>: View{
    var content: Content
    var title: String
    var rect: CGRect

    init(rect: CGRect ,title: String, @ViewBuilder content: @escaping ()->Content){
        self.content = content()
        self.title = title
        self.rect = rect
    }

    @State var tag: Int = 1009

    var body: some View{
        Rectangle()
            .fill(.white.opacity(0.02))
            .onAppear {
                addOverlayView()
            }
            .onDisappear{
                removeOverlayView()
            }
    }

    //MARK: - Removing the overlay when over view disappeared
    func removeOverlayView(){ }

    //MARK: - Adding An Extra View over the Current View
    func addOverlayView(){ }

    @ViewBuilder
    func overlaySwiftUIView()->some View{ }

    //MARK: - Random Number for Tag
    func generateRandom()->Int{ }

CodePudding user response:

In your extension to View, in this line SpotlightView(rect: rect, content: title) you are not matching your initialiser. The SpotlightView requires 3 parameters:

  • rect, of type CGRect
  • title, of type String
  • content, a closure

You are providing:

  • rect, of type CGRect
  • content, of type String
  • a closure

Start by correcting the call to SpotlightView, replacing "content" with "title".

  • Related