Can I use an import alias?

In C #, when loading a library in which there are many name collisions with existing code, there is a way to import an alias, so you do not need to fully explain the namespace for each use. eg:

using MyCompany.MyLibrary.Model as MMM 

then you could do

 MMM.MyObject 

instead

 MyCompany.MyLibrary.Model.MyObject 

With a recent upgrade to swift 3.0, I found that some of my model objects interfere with Foundation types, and I was forced to prefix things that used to have the NS prefix in the class name with Foundation.classname . It would be great if I could enter the import alias of my model library in the same way as the above C # example. Is this possible in swift 3.0? If there is no other strategy to avoid name clashes, what would make it necessary to write a frame name before each type? I am thinking about returning to prefix class names, as it was in obj-c, but I am trying to examine my options before I do this.

+6
source share
1 answer

At all

You can import a specific object, as well as the entire module:

 import struct SomeModule.SomeStruct import class SomeModule.SomeClass import func SomeModule.someFunc 

See the complete list of types of "imported" types in the import-kind rule of Swift grammar .

Then you can create typealias :

 typealias SMSomeStruct = SomeModule.SomeStruct 

And, like in Swift 3, import declarations are not merged with aliasing.

Examination of Collisions with Foundation Entities

Say you have a class SomeModule.NumberFormatter .

It is enough to create two typealias es in a separate Swift file (in the import project) to prevent conflicts:

 import Foundation import SomeModule typealias NumberFormatter = Foundation.NumberFormatter typealias SMNumberFormatter = SomeModule.NumberFormatter 
+7
source

All Articles