SwiftUI 中的错误消息:Type '() -> ()' cannot conform to 'ShapeStyle'

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

我在应用程序里面写了一个指令的扩展,写完后出现错误:

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

这是扩展本身,这是错误:

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{ }
}

显示指令所需的结构

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{ }

在您对 View 的扩展中,在这一行 SpotlightView(rect: rect, content: title) 中,您没有匹配您的初始化程序。 SpotlightView需要3个参数:

  • 矩形,类型为 CGRect
  • 标题,字符串类型
  • 内容,一个闭包

您正在提供:

  • 矩形,类型为 CGRect
  • 内容,字符串类型
  • 闭包

首先更正对 SpotlightView 的调用,将“内容”替换为“标题”。