Namespacing in Swift

Which scope are you talking about?

You can have multiple functions that have the same name. There are some situations where you would probably actually want to do that.

This article explores Namespacing in Swift.

Image for post
Image for post
Photo by Jon Tyson on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE)
  • Some knowledge of encapsulation (guide HERE)
  • Knowledge of structs and classes (guide HERE)
  • Knowledge of Access Control is useful when Frameworks are invovled later in the article (guide HERE)

Terminology

Frameworks: A way of sharing a package of code with Apps, team members or further afield

Module: A framework or application built and shipped as a single unit that can be imported by another module with Swift’s import keyword. Equivalent to node modules, packages, gems or jars in other languages

Namespace: A named region of a program used to group variables, types and methods

Struct: An object defined in Swift, using pass by value semantics

Target: A development target contains a set of source files that are compiled into a module or test suite

Advantages of Namespaces

Using Namespaces can give us the following advantages:

  • Preventing name collisions
  • Provides some form of data encapsulation

So now let us get stuck in learning about Namespaces in Swift

Implicit namespacing

Namespace is not on a file bases, rather they are per target.

This, in effect means that we don’t need a Module prefix, so if we want to use the Integer type we are fine!

let myNum: Int = 4

which saves us from specifying that we are using Swift’s version

let myNum: Swift.Int = 4

now, in the same file what if we create our own rather silly Int type

struct Int {
var int: Swift.Int
}

Now Int will refer to our Int, that is the conflict is resolved as the

let myNum: Int = Int(int: 4)

So in a view controller this would look like the following:

class ViewController: UIViewController {
struct Int {
var int: Swift.int
}
override func viewDidLoad() {
super.viewDidLoad()
let myNum: Int = Int(int: 3)
print ("\(myNum)")
}
}

Namespacing with a Framework

Namespace with a Framework means that we need to make the initializer public across modules

struct Int {
var int: Swift.Int
public init(int: Swift.Int) {
self.int = int
}
}

This can then be accessed from the main view controller if the Framework is imported, that is:

import UIKit
import IntFrameworkOne
class ViewController: UIViewController {
override func viewdidLoad() {
super.viewDidLoad()
let myNum: Int = Int(int: 4)
print (myNum)
}
}

therefore Int is taken from the Framework.

Namespacing with Two Frameworks

Namespace with two Frameworks that contain the same Int.

So at the top of the view controller we import both Frameworks and then proceed as before

import UIKit
import IntFrameworkOne
import IntFrameworkTwo
class ViewController: UIViewController {
override func viewdidLoad() {
super.viewDidLoad()
let myNum: Int = Int(int: 4)
print (myNum)
}
}

This gives an error:

‘Int’ is ambiguous for type lookup in this context

which, of course, makes perfect sense.

So we can specify which Framework that we mean

import UIKit
import IntFrameworkOne
import IntFrameworkTwo
class ViewController: UIViewController {
override func viewdidLoad() {
super.viewDidLoad()
let myNum: IntFrameworkOne.Int = Int(int: 4)
print (myNum)
}
}

This works nicely!

Conclusion:

Namespacing is a way that scope is managed in your project. It is certainly worth getting to grips with, particularly if you have multiple Modules or Frameworks that you need to manage.

This article should begin to help you out and give you a grip on the challenges that will face you as you expand your code across files and more.

Extend your knowledge

  • You can read about using local frameworks in the form of CocoaPods HERE

The Twitter contact:

Any questions? You can get in touch with me here

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store