Swizzling in Swift

Change the Implementation at Runtime

Steven Curtis
2 min readAug 22, 2022
Photo by Javier Allegue Barros on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

This article has been developed using Xcode 11.5, and Swift 5.2.4

Terminology:

Runtime: The period of time where a program is running

Swizzling: Method Swizzling is the process of changing the implementation of an existing selector

Swizzling is called Monkey Patching in some languages, and this moves us towards understanding the attitude of the language feature. Furthermore, some people call it a hack or the nonsensical insult of an anti-pattern.

We are going to explore how this can be used in Swift, and a real example of the use in Swift

Swizzling under the hood

Method Swizzling is about changing the implementation of an existing selector at runtime. This is performed through Swift’s Witness table and the mapping of the underlying functions contained within.

The example project

--

--

Responses (1)