1

I'm learning Swift and I want to import a protocol from one file into another. I've looked at this thread 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.

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
icicleking
  • 1,029
  • 14
  • 38

1 Answers1

1

I found the answer in this blog post: https://monospacedmonologues.com/2019/01/running-swift-without-xcode/

Essentially, the answer is to compile the code with swiftc including a "dummy main file." And define the main function in the code you want to run:

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

In main.switf:

main()

In my_file.swift

func main() {
// the main functionality
}

compile like this:

swiftc -o myprogram my_file.swift Helpers/*.swift
icicleking
  • 1,029
  • 14
  • 38