如何在立即模式下包含来自其他.swift文件的.swift文件?

时间:2021-06-03 20:52:12

Having a file with function definition bar.swift:

有一个带有函数定义bar.swift的文件:

func bar() {
    println("bar")
}

And a script to be run in immediate mode foo.swift:

以及立即模式foo.swift运行的脚本:

#!/usr/bin/xcrun swift -i
bar()

How do you import bar.swift's bar() function from foo.swift?

如何从foo.swift导入bar.swift的bar()函数?

2 个解决方案

#1


25  

I think the answer right now is that you can't split code across multiple files unless you compile the code. Executing with #!/usr/bin/swift is only possible for single file scripts.

我认为现在的答案是除非编译代码,否则不能跨多个文件分割代码。使用#!/ usr / bin / swift执行只能用于单个文件脚本。

It's obviously a good idea to file an enhancement request at http://bugreport.apple.com/, but in the mean time you're going to have to compile the code before executing it.

在http://bugreport.apple.com/上提交增强请求显然是一个好主意,但与此同时,您必须在执行代码之前编译代码。

Also, your foo.swift file cannot have that name, you have to rename it to main.swift. If there are multiple files being complied, then only main.swift is allowed to have code at the top level.

此外,您的foo.swift文件不能具有该名称,您必须将其重命名为main.swift。如果有多个文件被编译,那么只允许main.swift在顶层有代码。

So, you have these two files:

所以,你有这两个文件:

main.swift:

main.swift:

bar()

bar.swift:

bar.swift:

func bar() {
    println("bar")
}

And compile/execute the code with:

并编译/执行代码:

$ swiftc main.swift bar.swift -o foobar

$ ./foobar 
bar

If all your swift files are in the same directory, you could shorten the compile command:

如果所有swift文件都在同一目录中,则可以缩短编译命令:

$ swiftc *.swift -o foobar

Or if you want to search child directories:

或者,如果要搜索子目录:

$ find . -iname '*.swift' | xargs swiftc -o foobar

#2


0  

Write a bash script to concatenate the files. The script below pre-pends the library file to the front of your script before execution:

编写一个bash脚本来连接文件。下面的脚本在执行之前将库文件预先挂起到脚本的前面:

#!/bin/bash

cat $HOME/my_swift/my_library_to_add.swift $1.swift > t.swift
swift t.swift

As the resulted file is the single use, you can place it on the RAM drive. Here is the more advanced version that creates or reuses the tiny 1 Mb RAM drive as required.

由于结果文件是单次使用,您可以将其放在RAM驱动器上。这是更高级的版本,可根据需要创建或重用小型1 Mb RAM驱动器。

if [ ! -d /Volumes/swift_buffer ]; then
   diskutil erasevolume HFS+ 'swift_buffer' `hdiutil attach -nomount ram://2048`
fi

cat $HOME/my_swift/my_library_to_add.swift $1.swift > /Volumes/swift_buffer/t.swift   
swift /Volumes/swift_buffer/t.swift

This creates a tiny RAM drive with the capacity of 1 Mb only, big enough for any utility script and simple library.

这样就创建了一个只有1 Mb容量的小型RAM驱动器,足以容纳任何实用程序脚本和简单的库。

The mounted RAM drive will be visible in the Finder, from where it can be ejected. I do not dispose it directly in the script as allocating takes time.

安装的RAM驱动器将在Finder中可见,从中可以弹出。我没有直接在脚本中处理它,因为分配需要时间。

#1


25  

I think the answer right now is that you can't split code across multiple files unless you compile the code. Executing with #!/usr/bin/swift is only possible for single file scripts.

我认为现在的答案是除非编译代码,否则不能跨多个文件分割代码。使用#!/ usr / bin / swift执行只能用于单个文件脚本。

It's obviously a good idea to file an enhancement request at http://bugreport.apple.com/, but in the mean time you're going to have to compile the code before executing it.

在http://bugreport.apple.com/上提交增强请求显然是一个好主意,但与此同时,您必须在执行代码之前编译代码。

Also, your foo.swift file cannot have that name, you have to rename it to main.swift. If there are multiple files being complied, then only main.swift is allowed to have code at the top level.

此外,您的foo.swift文件不能具有该名称,您必须将其重命名为main.swift。如果有多个文件被编译,那么只允许main.swift在顶层有代码。

So, you have these two files:

所以,你有这两个文件:

main.swift:

main.swift:

bar()

bar.swift:

bar.swift:

func bar() {
    println("bar")
}

And compile/execute the code with:

并编译/执行代码:

$ swiftc main.swift bar.swift -o foobar

$ ./foobar 
bar

If all your swift files are in the same directory, you could shorten the compile command:

如果所有swift文件都在同一目录中,则可以缩短编译命令:

$ swiftc *.swift -o foobar

Or if you want to search child directories:

或者,如果要搜索子目录:

$ find . -iname '*.swift' | xargs swiftc -o foobar

#2


0  

Write a bash script to concatenate the files. The script below pre-pends the library file to the front of your script before execution:

编写一个bash脚本来连接文件。下面的脚本在执行之前将库文件预先挂起到脚本的前面:

#!/bin/bash

cat $HOME/my_swift/my_library_to_add.swift $1.swift > t.swift
swift t.swift

As the resulted file is the single use, you can place it on the RAM drive. Here is the more advanced version that creates or reuses the tiny 1 Mb RAM drive as required.

由于结果文件是单次使用,您可以将其放在RAM驱动器上。这是更高级的版本,可根据需要创建或重用小型1 Mb RAM驱动器。

if [ ! -d /Volumes/swift_buffer ]; then
   diskutil erasevolume HFS+ 'swift_buffer' `hdiutil attach -nomount ram://2048`
fi

cat $HOME/my_swift/my_library_to_add.swift $1.swift > /Volumes/swift_buffer/t.swift   
swift /Volumes/swift_buffer/t.swift

This creates a tiny RAM drive with the capacity of 1 Mb only, big enough for any utility script and simple library.

这样就创建了一个只有1 Mb容量的小型RAM驱动器,足以容纳任何实用程序脚本和简单的库。

The mounted RAM drive will be visible in the Finder, from where it can be ejected. I do not dispose it directly in the script as allocating takes time.

安装的RAM驱动器将在Finder中可见,从中可以弹出。我没有直接在脚本中处理它,因为分配需要时间。