Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 184.108.40.206 release ready!
Updated 31 March 2022
You will be thinking that what is the Method Dispatch in swift.
We will learn about this in this article, so read this article till the end to explore something new in Swift.
Ever wondered what happens when you call a method in swift?
How does your compiler come to the conclusion to call that particular method from the different child classes? And more importantly, how does the compiler knows where the called method resides.
All this happens both at the compile time and run time with the help of the method dispatch.
Generally, there are three types of method dispatch, and the calling of any method is based on these three types, whether the method call is from class type or value type.
We will now learn about these three methods dispatch now.
Static dispatch is the fastest method dispatch used in swift. We are calling it the fastest because in static method dispatch there is no method overriding available, hence there would be only one implementation of the method and the method will be residing at a single location in the memory.
You can use the static method dispatch by keywords such as static and final.
It is the default method dispatch for the value types as the value types cannot be overridden.
This is the default method dispatch used in Swift.
For classes, which use the inheritance this method of dispatch creates a look-up table at the compile time. The look-up table contains the addresses of the methods implemented in the classes.
During runtime, this lookup table is held as an array of addresses to the actual location in the memory.
In other words, using this look-up table helps in calling the method in the child class or in the parent class.
The look-up table or the witness table is computed at the compile time. Every class either the child class or the parent class held this witness table that contains the addresses of the methods and these addresses are used at the run-time to call the method.
To know more you can use the below command to how the look-up table is created at the compile time.
As we know that Objective-C is a run-time flexible language, by run-time flexible means we can change the implementation of any method at the run-time.
This creates a problem when creating a look-up table during compile time. The look-up table generated at the compile-time may not represent the correct implementation of the method that needs to be called at run-time as it may have get changes due to the run-time flexibility of the Objective-C.
Message dispatch maintains a different table at runtime. A lookup happens on this table at runtime to figure out the actual method address.
Methods which use the dynamic dispatch are not listed in the look-up table at the compile time because these methods can be changed due to Objective-C run-time flexibility.
We declare those methods by using the keywords dyanmic and @objc
Method dispatch is the way to increase the performance of your application.
In conclusion, you can visit the official Apple documentation here
Please visit here to read my other articles.
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.