Swift REPL:如何导入、加载、评估或需要 .swift 文件? [英] Swift REPL: how to import, load, evaluate, or require a .swift file?

查看:20
本文介绍了Swift REPL:如何导入、加载、评估或需要 .swift 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swift REPL 中,如何导入(即加载、评估、要求)一个典型的文本 *.swift 文件?

In the Swift REPL, how to import (a.k.a. load, evaluate, require) a typical text *.swift file?

  • 我想使用这个文件中的代码:~/src/Foo.swift

这样的语法不起作用:import ~/src/Foo.swift

比较:

  • 框架的 Swift REPL 中的等效解决方案是:import Foundation

Ruby REPL 中针对 *.ruby 文件的等效解决方案是:require "~/src/foo"

An equivalent solution in the Ruby REPL for a *.ruby file is: require "~/src/foo"

这些是/不是/我要问的类似问题:

These are similar questions that are /not/ what I'm asking:

  • 如何使用/制作 Swift 命令行脚本、可执行文件、模块等

  • How to use/make a Swift command-line script, executable, module, etc.

如何使用/制作 XCode 游乐场、项目、库、框架等

How to use/make an XCode playground, project, library, framework, etc.

如何使用预先存在的文件列表启动 REPL.

How to launch the REPL with a pre-existing list of files.

推荐答案

你需要使用 -I,所以如果你的 modulename.swiftmodule 文件在 ~/mymodules 中,那么使用

You need to use -I, so if your modulename.swiftmodule file is in ~/mymodules than launch swift with

swift -I ~/mymodules

然后你就可以导入你的模块了

and then you will be able to import your module

import module name

应该就这么简单

这篇关于Swift REPL:如何导入、加载、评估或需要 .swift 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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