Site is currently on development mode

Swift Import Declarations

Last updated 1 year ago by Mat tt


One of the first lessons we learn as software developers is how to organize concepts and functionality into discrete units. At the smallest level, this means thinking about types and methods and properties. These pieces then form the basis of one or more modules, which may then be packaged into libraries or frameworks.

In this way, import declarations are the glue that holds everything together.

Yet despite their importance, most Swift developers are familiar only with their most basic form:


import <#module#>

This week on NSHipster, we’ll explore the other shapes of this most prominent part of Swift.

An import declaration allows your code to access symbols that are declared in other files. However, if more than one module declares a function or type with the same name, the compiler may not be able to tell which one you want to call in code.

To demonstrate this, consider two modules representing the multisport competitions of Triathlon and Pentathlon:

A triathlon consists of three events: swimming, cycling, and running.


``` // Triathlon Module func swim() { print("🏊‍ Swim 1.5 km") }

func bike() { print("🚴 Cycle 40 km") }

func run() { print("🏃‍ Run 10 km") } ```

Read full Article