Learn Swift ‘Import’ Statements. Usages that junior developers don’t know about
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.
Import Specific Module
For reference, UIViewController is the submodule of UIKit.
Import class, enum, func, protocol, struct, typealias, var
Let’s assume that there’s a framework with these
Now you can import like this
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.
- Like many other languages, Swift can also import a specific type.
- If name collision occurs, specify where you are importing from.