8

I am writing a Swift script and I want to keep it clean. The way I would do this is by create multiple files and importing them. However, I can't figure out how to import another file into a Swift script. I am not using Xcode.

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92

1 Answers1

13

There's currently no way to import other swift files in a interpreted swift script. But you can concatenate all source files before executing them:

cat one.swift two.swift three.swift | swift -

If you're using the swift compiler, just add the files you want to compile together before the -o argument:

swiftc one.swift two.swift three.swift -o combined

No need to import them, they are already in the same module.

redent84
  • 18,901
  • 4
  • 62
  • 85
  • Piping did not work for me, but concatenating into file and then running it (same bash script) seems possible. – Audrius Meškauskas Jul 14 '17 at 15:02
  • I'm trying to get a console app working (no XCode). The `swiftc` with all of my files that have classes worked. I had to rename my main driver file to `main.swift` to have top level functions. – Benyam Ephrem May 08 '20 at 00:21