SwiftUI/AppWindowsController.swift:89:致命错误

问题描述 投票:0回答:1

有人见过这个错误吗?

SwiftUI/AppWindowsController.swift:89: Fatal error 

查遍谷歌并没有找到太多相关信息。我正在尝试为 Macos 开发一个简单的菜单栏应用程序,每当我尝试运行我的应用程序时都会遇到此错误。我认为这可能是 AppStorage 的一个错误,但我不确定。我还在 Info.plist 文件中将布尔键应用程序是代理(UI 元素)设置为“是”。该键根据 apple 文档指定以下内容:

LSUIElement (Boolean - macOS) specifies whether the app runs as an agent app. If this key is set to YES, Launch Services runs the app as an agent app. Agent apps do not appear in the Dock or in the Force Quit window. Although they typically run as background apps, they can come to the foreground to present a user interface if desired. A click on a window belonging to an agent app brings that app forward to handle events.

我还想知道这个键是否会导致我的应用程序因错误而崩溃,但我已将值切换为

YES
NO
,并且应用程序会因设置任一值而崩溃。

我正在运行 Xcode 15.2 和 Swift 5.9.2。最低部署版本是 MacOS 14.2。

这是我的完整申请代码:

import SwiftUI

@main
struct GiveMeTwentyApp: App {
    
    @AppStorage(SettingsKeys.showAppInMenuBar) var showAppInMenuBar: Bool = true
    
    var body: some Scene {
        #if os(macOS)
        MenuBarExtra(isInserted: $showAppInMenuBar) {
            ContentView()
                .navigationTitle("GiveMeTwenty")
        } label: {
            Label("GiveMeTwenty", systemImage: "star")
        }
        .menuBarExtraStyle(.window)
        #endif
        
        WindowGroup {
            ContentView()
                .navigationTitle("GiveMeTwenty")
        }
    }
}
import Foundation

enum SettingsKeys {
    static let reminderFrequency = "reminderFrequency"
    static let runWhenComputerStarts = "runWhenComputerStarts"
    static let showAppInMenuBar = "showAppInMenuBar"
    static let popUpMenuMessage = "popUpMenuMessage"
    static let notificationsEnabled = "notificationsEnabled"
}
import SwiftUI

struct ContentView: View {
    // Configure app to send reminders every x hours
    @AppStorage(SettingsKeys.reminderFrequency) var reminderFrequency: Int = 1
    
    // Configure app to send notifications or not
    @AppStorage(SettingsKeys.notificationsEnabled) var notificationsEnabled: Bool = true
    
    // Configure app to run when computer starts
    @AppStorage(SettingsKeys.runWhenComputerStarts) var runWhenComputerStarts: Bool = true
    
    // Configure app to appear in menu bar or not
    @AppStorage(SettingsKeys.showAppInMenuBar) var showAppInMenuBar: Bool = true
    
    // Configure message to show on popUp Menu
    @AppStorage(SettingsKeys.popUpMenuMessage) var popUpMenuMessage: String = "Give Me Twenty!"
    
    private let reminderFrequencyOptions = [1, 2, 3, 4, 5, 6, 7, 8]
    
    var body: some View {
        VStack(alignment: .leading) {
            HStack {
                Picker("Every", selection: $reminderFrequency) {
                    ForEach(reminderFrequencyOptions, id: \.self) {
                        Text($0.description)
                    }
                }
                .frame(width: 80)
                .clipped()
                
                Text("hours take a break.")
                
            }
            .padding()
            
            Toggle(isOn: $notificationsEnabled) {
                Text("Enable notifications")
            }
            .toggleStyle(.checkbox)
            .padding()
            
            Toggle(isOn: $runWhenComputerStarts) {
                Text("Run when computer starts")
            }
            .toggleStyle(.checkbox)
            .padding()
            
            Toggle(isOn: $showAppInMenuBar) {
                Text("Show app in menu bar")
            }
            .toggleStyle(.checkbox)
            .padding()
            
        }
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
        .padding()
    }
}
ios xcode macos swiftui
1个回答
0
投票

删除这些行:

        WindowGroup {
            ContentView()
                .navigationTitle("GiveMeTwenty")
        }

因为您只想在用户点击菜单栏图标时显示 UI。

请注意,这不是问题

WindowGroup
本身,因为如果您注释掉
MenuBarExtra
块,它可以工作,但不能作为仅菜单栏的应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.