Is there a way for a Swift script to use multiple files

I am trying to write a script with Swift (not an Xcode project). To be clear, the first line of my file

#!/usr/bin/swift 

And I just call it from the command line.

However, I cannot figure out how to use this script code, which is in another .swift file. He does not select it from the same directory, and I do not see the import method.

Is it supported?

+7
command-line swift
source share
3 answers

My current solution is a simple shell script that merges all files into one and executes a concatenated file:

 TMPFILE=`mktemp /tmp/Project.swift.XXXXXX` || exit 1 trap "rm -f $TMPFILE" EXIT cat *.swift > $TMPFILE swift $TMPFILE 
+3
source share

I am using Marian's solution variant:

 cat A.swift B.swift main.swift | swift - 
+13
source share

The interesting thing is that even as a compiled language, any custom classes should be defined above any use for this class. that is, in the solution above, if "B.swift" uses the class defined in "A.swift", first "A.swift" is needed. It was my own experience running from the command line, anyway

0
source share

All Articles