Discover how Swift determines method execution at runtime, enhancing your understanding of performance and code efficiency.Discover how Swift determines method execution at runtime, enhancing your understanding of performance and code efficiency.

A Traveler's Guide to Method Dispatch in Swift

2025/12/03 06:18
4 min read
For feedback or concerns regarding this content, please contact us at crypto.news@mexc.com

Introduction

Method dispatch is an algorithm used to select the appropriate method that needs to be invoked upon a call. The primary goal of method dispatch is to provide the program with information on where it can find the executable code for a specific method in memory.

Types of Method Dispatch

Compiled languages have three types of method dispatch:

  • Static or direct dispatch
  • Table or virtual dispatch
  • Message dispatch

Static Dispatch

Static dispatch is the fastest dispatch method in Swift. Since there is no method overriding available, there is only one implementation of the method, and it resides at a single location in memory.

\ We can use static dispatch using keywords such as static ,final, private.

\ Static dispatch is a default method dispatch for the value types since the value types can’t be overridden.

\ Let’s look at some examples:

Final Keyword

Once we add the final keyword to a class, its methods do not support overriding, and this is when static dispatch comes into play.

// MARK: Final class final class ClassExample { // MARK: Static dispatch func method() { // implementation ... } }

Protocol Extension

Once you add a default implementation of a protocol using an extension, its dispatch method switches to static dispatch instead of using a Witness Table.

// MARK: Prorocol Extension extension ProtocolExample { // MARK: Direct Dispatch func method() { // implementation ... } } class ClassExample2: ProtocolExample {} let classExample2 = ClassExample2() classExample2.method()

Class Extension

When a method is implemented in an extension, it means it can’t be overridden by subclasses. In this case, there is room for static dispatch.

// MARK: Example Class Extension class ClassExample3 {} extension ClassExample3 { // MARK: Direct Dispatch func method() { // implementation ... } } let classExample3 = ClassExample3() classExample3.method()

Access Control

We can’t access a private method outside of the class body. This means that the method can’t be overridden and uses static dispatch.

// MARK: Access Control class ClassExample4 { // MARK: Direct Dispatch private func method() { // implementation ... } }

Table Dispatch

Table dispatch is used when we have to deal with inheritance. This is a default type of dispatch used in Swift.

Virtual Table

For each instance of a class or subclass, a virtual table is created that contains information about implemented methods for each class and stores a reference to the appropriate implementation. The main disadvantage of virtual table dispatch is that it has a lower speed than static dispatch.

\ Let’s look at an example:

// MARK: Virtual Table class ParentClass { func method1() {} func methdod2() {} } class ChildClass: ParentClass { override func method1() {} func method3() {} }

\ For each instance, its own virtual table is created as follows:

\

Witness Table

A Witness Table is used by protocols and is created for each class that conforms to the protocol. The CPU uses this table to determine where it should look for an appropriate implementation. Each type (value and reference) that conforms to a protocol has its own Protocol Witness Table, which contains pointers to the methods of the type required by the protocol.

\ Let’s look at an example:

// MARK: Witness Table Dispatch protocol ProtocolExample { func method1() func method2() } class ClassExample1: ProtocolExample { func method1() {} func method2() {} } class ClassExample2: ProtocolExample { func method1() {} func method2() {} }

\ In this case, a witness table is created for each class:

\

Message Dispatch

Message Dispatch is the most dynamic method dispatch style. It looks for an appropriate implementation during runtime. Because it operates during runtime, we can use Method Swizzling to change method implementations.

\ If you want to use message dispatch, you need to add @objc dynamic before a method implementation.

// MARK: Message Dispatch class ClassExample: NSObject { @objc dynamic func method() {} } class SubClassExample: ClassExample { @objc dynamic override func method() {} } let subclass = SubClassExample() subclass.method()

\ The implementation of the method is searched for within SubClassExample. If there is no implementation of this method in that class, the search continues in the parent class, and so on until it reaches NSObject.

\

Let’s combine all the types into a single table:

\

Conclusion

In summary, method dispatch in Swift is a critical aspect of code execution, impacting performance and flexibility. By choosing the right dispatch method, developers can optimize their code, ensure adaptability, and leverage Swift’s dynamic features effectively. Understanding and mastering method dispatch is essential for building efficient and adaptable Swift applications.

Disclaimer: The articles reposted on this site are sourced from public platforms and are provided for informational purposes only. They do not necessarily reflect the views of MEXC. All rights remain with the original authors. If you believe any content infringes on third-party rights, please contact crypto.news@mexc.com for removal. MEXC makes no guarantees regarding the accuracy, completeness, or timeliness of the content and is not responsible for any actions taken based on the information provided. The content does not constitute financial, legal, or other professional advice, nor should it be considered a recommendation or endorsement by MEXC.

You May Also Like

Vitalik Buterin to Ethereum Developers: Build It Like It Has to Last Without You

Vitalik Buterin to Ethereum Developers: Build It Like It Has to Last Without You

Key Takeaways Vitalik Buterin wants Ethereum apps built to survive without developers, corporate servers, or trusted third parties Two major […] The post Vitalik
Share
Coindoo2026/03/07 15:49
Non-Opioid Painkillers Have Struggled–Cannabis Drugs Might Be The Solution

Non-Opioid Painkillers Have Struggled–Cannabis Drugs Might Be The Solution

The post Non-Opioid Painkillers Have Struggled–Cannabis Drugs Might Be The Solution appeared on BitcoinEthereumNews.com. In this week’s edition of InnovationRx, we look at possible pain treatments from cannabis, risks of new vaccine restrictions, virtual clinical trials at the Mayo Clinic, GSK’s $30 billion U.S. manufacturing commitment, and more. To get it in your inbox, subscribe here. Despite their addictive nature, opioids continue to be a major treatment for pain due to a lack of effective alternatives. In an effort to boost new drugs, the FDA released new guidelines for non-opioid painkillers last week. But making these drugs hasn’t been easy. Vertex Pharmaceuticals received FDA approval for its non-opioid Journavx in January, then abandoned a next generation drug after a failed clinical trial earlier this summer. Acadia similarly abandoned a promising candidate after a failed trial in 2022. One possible basis for non-opioids might be cannabis. Earlier this year, researchers at Washington University at St. Louis and Stanford published a study showing that a cannabis-derived compound successfully eased pain in mice with minimal side effects. Munich-based pharmaceutical company Vertanical is perhaps the furthest along in this quest. It is developing a cannabinoid-based extract to treat chronic pain it hopes will soon become an approved medicine, first in the European Union and eventually in the United States. The drug, currently called Ver-01, packs enough low levels of cannabinoids (including THC) to relieve pain, but not so much that patients get high. Founder Clemens Fischer, a 50-year-old medical doctor and serial pharmaceutical and supplement entrepreneur, hopes it will become the first cannabis-based painkiller prescribed by physicians and covered by insurance. Fischer founded Vertanical, with his business partner Madlena Hohlefelder, in 2017, and has invested more than $250 million of his own money in it. With a cannabis cultivation site and drug manufacturing plant in Denmark, Vertanical has successfully passed phase III clinical trials in Germany and expects…
Share
BitcoinEthereumNews2025/09/18 05:26
Short-term profit-taking pushes Bitcoin back below key $70K level – What next?

Short-term profit-taking pushes Bitcoin back below key $70K level – What next?

The post Short-term profit-taking pushes Bitcoin back below key $70K level – What next? appeared on BitcoinEthereumNews.com. Bitcoin [BTC] rallied as high as $74
Share
BitcoinEthereumNews2026/03/07 16:09