Questions And Answers

More Tutorials

Swift Imports

The import directive can be used to import definitions from another Swift file.

For example, a Swift script might contain this:

import "defs";
file f;

which would import the content of defs.swift:

type file;
Imported files are read from two places. They are either read from the path that is specified from the import command,such as:

import "definitions/file/defs";

or they are read from the environment variable SWIFT_LIB. This environment variable is used just like the PATH environment variable. For example, if the command below was issued to the bash shell:

export SWIFT_LIB=${HOME}/Swift/defs:${HOME}/Swift/functions

then the import command will check for the file defs.swift in both "${HOME}/Swift/defs" and "${HOME}/Swift/functions" first before trying the path that was specified in the import command.

Other valid imports:

import "../functions/func"
import "/home/user/Swift/definitions/defs"

There is no requirement that a module is imported only once. If a module is imported multiple times, for example in different files, then Swift will only process the imports once.

Imports may contain anything that is valid in a Swift script, including the code that causes remote execution.


In this page (written and validated by ) you learned about Swift Imports . What's Next? If you are interested in completing Swift tutorial, your next topic will be learning about: Swift Mappers.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.