Der*_*328 9 command-line module read-eval-print-loop swift
我正在使用命令行创建swift文件并使用"swift"命令来运行其中一个.但是,我希望一个文件能够从另一个文件访问函数.如果这是C,那么我可以使用#include宏并指定文件的位置.但斯威夫特的进口声明似乎并不允许这样.应该有办法,我想知道如何做到这一点.
例如:
如果我有一个包含函数的文件,然后我创建另一个使用该函数的文件.我如何允许它使用它?
// file1.swift
import Foundation
func sayHello() -> String {
return "hello"
}
// file2.swift
import file1 // <-- trying to import file1 but it doesn't work
println(sayHello())
Run Code Online (Sandbox Code Playgroud)
一旦文件生成,我在终端写"swift file2.swift".但它告诉我..
error: no such module 'file1.swift'
Run Code Online (Sandbox Code Playgroud)
很明显,swift编译器正在寻找一个模块.如何将file1变为模块?我已经看到了一些解决方案,但它们都发生在Xcode中.我正在寻找的是在命令行中完成所有操作.
// file1.swift
func sayHello() -> String {
return "hello"
}
// main.swift
println(sayHello())
Run Code Online (Sandbox Code Playgroud)
然后从终端:
$ swiftc file1.swift main.swift
$ ./main
hello
Run Code Online (Sandbox Code Playgroud)
小智 -4
有关从命令行创建模块(Alfred 帮助程序库)的示例,请参阅http://computers.tutsplus.com/tutorials/alfred-workflows-in-swift--cms-21807 ,也可访问 http://www. alfredforum.com/topic/4902-alfred-workflows-in-swift-tutorial-is-available/?p=35662其中本教程的作者更详细地解释了四个步骤的编译过程。
| 归档时间: |
|
| 查看次数: |
1720 次 |
| 最近记录: |