OSX 没有故事板或 xib 文件的应用程序使用 Swift

OSX application without storyboard or xib files using Swift

不幸的是,我没有在 Internet 上找到任何有用的东西 - 我想知道,在 Swift 中,如果不使用故事板或 XIB 文件,我实际上必须键入哪些代码来初始化应用程序。我知道我必须有一个名为 main.swift 文件。但我不知道在那里写什么(比如我需要 autoreleasepool 或类似的东西吗?)。例如,我将如何初始化 NSMenu 以及如何将 NSViewController 添加到活动 window(iOS 的类似 .rootViewController 不帮助)。感谢您的帮助 ;)

编辑: 我实际上不想在 AppDelegate 前面使用 @NSApplicationMain。我宁愿知道那里到底发生了什么,然后自己去做。

如果您不想拥有@NSApplicationMain 属性,请执行:

  1. 有一个文件main.swift

  2. 添加以下顶级代码:

     import Cocoa
    
     let delegate = AppDelegate() //alloc main app's delegate class
     NSApplication.shared.delegate = delegate //set as app's delegate
     NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv) //start of run loop       
    
     // Old versions:
     //  NSApplicationMain(C_ARGC, C_ARGV)
     //  NSApplicationMain(Process.argc, Process.unsafeArgv);  
    

其余部分应该在您的应用委托中。例如:

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {
    var newWindow: NSWindow?
    var controller: ViewController?
    
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        newWindow = NSWindow(contentRect: NSMakeRect(10, 10, 300, 300), styleMask: .resizable, backing: .buffered, defer: false)
        
        controller = ViewController()
        let content = newWindow!.contentView! as NSView
        let view = controller!.view
        content.addSubview(view)
        
        newWindow!.makeKeyAndOrderFront(nil)
    }
}

那么你有一个viewController

import Cocoa

class ViewController : NSViewController {
    override func loadView() {
        let view = NSView(frame: NSMakeRect(0,0,100,100))
        view.wantsLayer = true
        view.layer?.borderWidth = 2
        view.layer?.borderColor = NSColor.red.cgColor
        self.view = view
    }
}

上面的顶级代码示例在 Xcode 的最新版本中不再有效。而是使用这个:

import Cocoa

let delegate = AppDelegate() //alloc main app's delegate class
NSApplication.shared().delegate = delegate //set as app's delegate

let ret = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

在 Swift 4 中它又发生了轻微的变化,

主文件必须有

import Cocoa

let delegate = AppDelegate()
NSApplication.shared.delegate = delegate

NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

A​​ppDelegate 必须

import Cocoa


class AppDelegate: NSObject, NSApplicationDelegate {
    var newWindow: NSWindow?
    var controller: ViewController?

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        newWindow = NSWindow(contentRect: NSMakeRect(10, 10, 300, 300), styleMask: .resizable, backing: .buffered, defer: false)

        controller = ViewController()
        let content = newWindow!.contentView! as NSView
        let view = controller!.view
        content.addSubview(view)

        newWindow!.makeKeyAndOrderFront(nil)
    }
}

视图控制器是一样的

我在 Swift 5,虽然我的应用程序可以使用 Noskcaj 的答案中的代码,但我发现现有 nib 文件中有一堆 [Nib Loading] Failed to connect... 错误。我通过在我的 main.swift 中使用它来修复它:

import Cocoa

autoreleasepool {
 let delegate = AppDelegate()
 // NSApplication delegate is a weak reference,
 // so we have to make sure it's not deallocated.
 withExtendedLifetime(delegate, {
     let application = NSApplication.shared
     application.delegate = delegate
     application.run()
     application.delegate = nil
 })
}

参考: