Home > database >  SwiftUI: Missing parameter in call -StateObject
SwiftUI: Missing parameter in call -StateObject

Time:11-05

I'm trying to access a class in my settings view

  struct SettingsView: View {
        @StateObject var stat = StatusBarController()

    .....}
       

But I'm getting an error for a missing parameter in StatusBarController()

Missing argument for parameter #1 in call

Insert '<#NSPopover#>'

StatusBarController Class

class StatusBarController: ObservableObject {
    private var statusBar: NSStatusBar
    private var popover: NSPopover
    private var eventMonitor: EventMonitor?
    private var content: ContentView?
    
    
    init(_ popover: NSPopover)
    {
        self.popover = popover
        statusBar = NSStatusBar.init()
      
        
        if let statusBarButton = statusItem.button {
        ........
        }
        
        eventMonitor = EventMonitor(mask: [.leftMouseDown, .rightMouseDown], handler: mouseEventHandler)
    }

.......}

CodePudding user response:

If you want to use default constructor for StateObject initialisation then provide default popover in controller init, like

class StatusBarController: ObservableObject {
    private var statusBar: NSStatusBar
    private var popover: NSPopover
    private var eventMonitor: EventMonitor?
    private var content: ContentView?
    
    
    init(_ popover: NSPopover = NSPopover())     // << here !!
    {
...

alternate is to make it optional (and update every dependency correspondingly)

  • Related