如果您不想拥有@NSApplicationMain属性,请执行以下操作:
- 有一个文件main.swift
- 添加以下顶级代码:
import Cocoa let delegate = AppDelegate() //alloc main app's delegate class NSApplication.sharedApplication().delegate = delegate //set as app's delegate // Old versions: // NSApplicationMain(C_ARGC, C_ARGV) NSApplicationMain(Process.argc, Process.unsafeArgv); //start of run loop
其余的应该在您的应用程序委托中。例如:
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 } }


