gpt4 book ai didi

xcode - 最小的 Cocoa/Swift 菜单栏应用程序不想运行

转载 作者:可可西里 更新时间:2023-11-01 00:16:23 24 4
gpt4 key购买 nike

我正在尝试在 Swift 中构建一个框架应用程序,我基本上只有一个菜单栏图标,没有窗口。从 Xcode 中的一个新的 Storyboard 项目开始,它最初可以工作,但是试图摆脱窗口,它似乎不想再运行了。我有以下内容:

import Cocoa
import AppKit

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

var window = NSWindow()
var statusBar = NSStatusBar.systemStatusBar()
var statusBarItem : NSStatusItem = NSStatusItem()

override func awakeFromNib() {
statusBarItem = statusBar.statusItemWithLength(-1)
statusBarItem.title = "Test"
}

func applicationDidFinishLaunching(aNotification: NSNotification) {
sleep(10);
}

func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}

}

在 AppDelegate.swift 中(基于 this tutorial )。通过 Xcode 运行它时,我收到一些警告:

2015-06-23 22:20:28.444 PENCloud[19491:3303755] Failed to connect (colorGridView) outlet from (NSApplication) to (NSColorPickerGridView): missing setter or instance variable
2015-06-23 22:20:28.444 PENCloud[19491:3303755] Failed to connect (view) outlet from (NSApplication) to (NSColorPickerGridView): missing setter or instance variable

从一些谷歌搜索中,似乎我应该能够忽略这些,但我的 statusBarItem 不再显示。我错过了什么?

最佳答案

你需要有如下代码的main.swift

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {

var statusBarItem : NSStatusItem!

func applicationDidFinishLaunching(aNotification: NSNotification) {
statusBarItem = statusBar.statusItemWithLength(-1)
statusBarItem.title = "Test"
}

func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}

}

autoreleasepool { () -> () in
let app = NSApplication.sharedApplication()
let delegate = AppDelegate()
app.delegate = delegate
app.run()
}

文件名必须是 main.swift。否则你会得到错误,Expressions is not allowed at the top level, on the line of autoreleasepool.

我在这里找到了答案: https://stackoverflow.com/a/26322464/338986

关于xcode - 最小的 Cocoa/Swift 菜单栏应用程序不想运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31012851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com