如何从命令行编译Swift

时间:2018-08-07 15:47:07

标签: swift macos cocoa command-line compilation

我有these 3 files

index.swift

import Cocoa

print("Init")
let app = NSApplication.shared()
let delegate = AppDelegate()  // alloc main app's delegate class
app.delegate = delegate      // set as app's delegate
app.run()
print("End")

src / delegate.swift

import Cocoa

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

    override init() {
        print("App")
        super.init()
        mainView()
    }

    func mainView() {
        print("View")
        newWindow   = NSWindow(contentRect: NSMakeRect(10, 10, 200, 200), styleMask: [.titled, .resizable, .closable], backing: .buffered, defer: false)
        let content = newWindow!.contentView! as NSView
        controller  = ViewController()
        //controller?.loadView()
        let view = controller!.view
        content.addSubview(view)
        newWindow!.makeKeyAndOrderFront(nil)
    }

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        print("Launch")  // Not showing this?
    }

    func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
        print("Bye")
        return true
    }
}

src / controller.swift

import Cocoa

class ViewController : NSViewController {
    var name = "Main"
    var button: NSButton?

    @IBAction func onClick(_: Any) {
        print("Click")
    }

    override func loadView() {
        print("Load")
        let view = NSView(frame: NSMakeRect(0,0,200,200))
        view.wantsLayer = true
        view.layer?.borderWidth = 2
        view.layer?.borderColor = NSColor.red.cgColor
        button = NSButton(frame: NSMakeRect(20, 20, 100, 32))
        button?.title = "Click Me"
        button?.target = self
        button?.action = #selector(onClick)
        view.addSubview(button!)
        self.view = view
    }
}

我试图像这样编译它:

swift index.swift src/delegate.swift src/controller.swift

但是出现此错误:

index.swift:7:16: error: use of unresolved identifier 'AppDelegate'
let delegate = AppDelegate()  // alloc main app's delegate class
               ^~~~~~~~~~~
Foundation.PortDelegate:1:17: note: did you mean 'PortDelegate'?
public protocol PortDelegate : NSObjectProtocol {
                ^
CoreText.CTRunDelegate:1:14: note: did you mean 'CTRunDelegate'?
public class CTRunDelegate {
             ^
<unknown>:0: warning: 'cacheParamsComputed' is deprecated
<unknown>:0: warning: 'cacheAlphaComputed' is deprecated
<unknown>:0: warning: 'keepCacheWindow' is deprecated
<unknown>:0: error: 'memoryless' is unavailable
Metal.MTLCommandBufferError:55:14: note: 'memoryless' has been explicitly marked unavailable here
        case memoryless
             ^

想知道如何进行编译。基本上,我只需要知道如何从当前定义的类AppDelegate和ViewController要求/导入这些类,将它们当前定义到index.swift的范围内。如果我将所有代码放入1个文件index.swift中,则可以正常编译。但是我想像在XCode中一样对它进行模块化,但是不调用XCode。想知道这是否可以通过某种plist解决。我尝试使用swift -Fswift -I指定导入目录,但没有成功。

1 个答案:

答案 0 :(得分:1)

正如@MartinR所说,swiftc是编译器。您的swift命令调用解释器,而不是编译器。我认为解释器只能运行一个文件。

我只浏览了以下几页,但它们可以帮助您运行包含多个文件的Swift程序。

  1. 使用简单的shell脚本将所有文件合并为一个,然后通过swift解释器运行合并的文件:

  2. 使用内置的Swift工具:

我通过Google找到了以下(以及许多类似的)页面:swift command line run multiple files