简体   繁体   中英

Swift - Import Source File by Path?

I've been playing around with using Swift as a scripting language, as described here . When executing script files from the terminal in this way, is there a way to include other Swift files by path (ie import /path/to/some/file.swift )? I'm aware of the import statement, but that doesn't seem to accept a file path. In Ruby I would use a require statement, but I don't know if there is a Swift equivalent to this.

import only works with modules . If you need to import a module which is located somewhere besides the normal import search path , you can add another directory to the search paths by passing the -I flag to the compiler:

-I <value> Add directory to the import search path

If you're looking at just a .swift file, you'll need to compile that into a module before you can import it from a separate module or the REPL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM