繁体   English   中英

Swift 从 Xcode 外部项目导入 class

[英]Swift import class from project outside Xcode

我正在学习 Swift,我想将协议从一个文件导入另一个文件。 我看过这个线程How do I import a Swift file from another Swift file? ,但所有答案都集中在设置target和其他 XCode 相关功能上。

I'm running swift files on the command line: swift myfile.swift , which is pretty clearly a different execution environment than Xcode. 关于导入相邻结构的任何指导?

myDir
├── CoolProtocol.swift
├── my file.swift

我在这篇博文中找到了答案: https://monospacedmonologues.com/2019/01/running-swift-without-xcode/

本质上,答案是用swiftc编译代码,包括一个“虚拟主文件”。 并在要运行的代码中定义main function:

├── Helpers
│   ├── CoolProtocol.swift
│   └── main.swift
├── my_file.swift

在 main.switf 中:

main()

在 my_file.swift

func main() {
// the main functionality
}

像这样编译:

swiftc -o myprogram my_file.swift Helpers/*.swift

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM