Swift’s standard library and name collision

Namespacing in Swift is implicit. All classes and other symbols belong to the target (module) they are defined in. So if you define a class String the fully qualified name would be MyTarget.String. When there is a name collision, you have to prefix the class name with the module (framework) it is defined in, except when there is a class with that name defined in the current module – this class takes precedence and does not need to be prefixed.

struct String {
    var swiftString = ""
}

var a = String()
var b = Swift.String()

So if you create your class TreeNode and Apple later adds a TreeNode as well, your name would take precedence if you are using only one module and you wouldn’t need to change anything. If you would want to use Swift’s TreeNode, you would need to refer to it as Swift.TreeNode.

Leave a Comment