Search code examples
swiftswift4

Is there any way to reference the current module in Swift?


Let's say I'm writing a swift module, and I want to name a type with a name which already exists. If the name is from another module, it's easy. I can just use the module name as a namespace:

import Foundation

class MyClass {
    class Notification : Foundation.Notification { ... }
}

My question is, is there any way to do the same with types in the same module? For example, I would like to be able to do something like this:

class Notification { ... }

class MyClass {
    class Notification : Module.Notification { ... }
}

Where Module.Notification is a reference to the type declared above. Is such a thing possible?


Solution

  • You need to use the actual name of your module:

    class Notification { ... }
    
    class MyClass {
        class Notification : MyAmazingTwitterApp.Notification { ... }
    }
    

    If you're working in Xcode, this defaults to your target name. There's a build setting "Product Module Name", under "Packaging" that lets you change this.

    If you're using the Swift build system, this is of course specified in your manifest file, via the PackageDescription.