简体   繁体   English

Swift 从 Xcode 外部项目导入 class

[英]Swift import class from project outside Xcode

I'm learning Swift and I want to import a protocol from one file into another.我正在学习 Swift,我想将协议从一个文件导入另一个文件。 I've looked at this thread How do I import a Swift file from another Swift file?我看过这个线程How do I import a Swift file from another Swift file? , but all the answers are focused on setting a target and other XCode related features. ,但所有答案都集中在设置target和其他 XCode 相关功能上。

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

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

I found the answer in this blog post: https://monospacedmonologues.com/2019/01/running-swift-without-xcode/我在这篇博文中找到了答案: https://monospacedmonologues.com/2019/01/running-swift-without-xcode/

Essentially, the answer is to compile the code with swiftc including a "dummy main file."本质上,答案是用swiftc编译代码,包括一个“虚拟主文件”。 And define the main function in the code you want to run:并在要运行的代码中定义main function:

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

In main.switf:在 main.switf 中:

main()

In my_file.swift在 my_file.swift

func main() {
// the main functionality
}

compile like this:像这样编译:

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

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

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