Learn Swift ‘Import’ Statements. Usages that junior developers don’t know about

KD Knowledge Diet
1 min readJul 27, 2022

Most swift developers are used to importing entire modules. They don’t think there’s a problem with that method, probably because of auto-import. And in most cases, it is. However, if you develop Swift Apps, you might encounter duplicate module name collision at least once in your lifetime.

What you can import in Swift

Instead of importing the entire module, you can import these items.

  • module
  • class
  • enum
  • func
  • protocol
  • struct
  • typealias
  • var

Import Specific Module

For reference, UIViewController is the submodule of UIKit.

Importing Submodule of UIKit

Import class, enum, func, protocol, struct, typealias, var

Let’s assume that there’s a framework with these

Imaginary Framework

Now you can import like this

Import protocol, class, enum, struct, typealias, var, func

What if frameworks have types with the same name?

When importing multiple modules, each module may have the same type name or the same name as a local type. In this case, you must write all the modules and types to avoid compilation errors.

Avoid Name Collision

Conclusion

  • Like many other languages, Swift can also import a specific type.
  • If name collision occurs, specify where you are importing from.

--

--

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!