有一种方法让Swift脚本使用多个文件 [英] Is there a way to have a Swift script use multiple files

查看:217
本文介绍了有一种方法让Swift脚本使用多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Swift写一个脚本(不是一个Xcode项目)。要清楚,我的文件的第一行是

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

 #!/usr/bin/swift

我只是从命令行调用它。

And I am just calling it from the command-line.

但是,我不知道如何让该脚本使用另一个.swift文件中的代码。它不会从同一个目录中找到它,并且没有办法 import ,我可以看到。

However, I can't figure out how to have that script use code that is in another .swift file. It doesn't pick it up from the same directory and there is no way to import that I can see.

推荐答案

我当前的解决方案是一个简单的shell脚本,它将所有文件合并成一个文件并执行连接文件:

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

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

这篇关于有一种方法让Swift脚本使用多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆