如何在 Swift 没有 Xcode IDE 的情况下发展

How to develop in Swift without Xcode IDE

我想将一个可运行的脚本和一个 class 放在一起,以便从一个单独的模块中包含。

如果我像这样将 class 放入脚本中,它就可以工作。

#!/usr/bin/env swift
class HelloWorld {
    func greet() {
        print("Hello World")
    }
}
var h = HelloWorld()
h.greet()

但是,当我将 HelloWorld class 放入 HelloWorld.swift 模块时,我没有找到让它工作的解决方案。

您只需要做:

$ swift HelloWorld.swift

还有你HelloWorld.swift:

class HelloWorld {
    func greet() {
        print("Hello World")
    }
}
var h = HelloWorld()
h.greet()

重要:从Swift 1.2 开始,您只能有一个 大脚本文件。 Reference