Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic modifier in functions in Swift

According to Apple :

When you mark a member declaration with the dynamic modifier, access to that member is always dynamically dispatched. Because declarations marked with the dynamic modifier are dispatched using the Objective-C runtime, they’re implicitly marked with the @objc attribute.

According to Wikipedia:

dynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time.

Dynamic dispatch is often used in object-oriented languages when different classes contain different implementations of the same method due to common inheritance. For example, suppose you have classes A, B, and C, where B and C both inherit the method foo() from A. Now suppose x is a variable of class A. At run time, x may actually have a value of type B or C and in general you can't know what it is at compile time.

Right now, I'm studying the dependency injection framework : Typhoon and when I open the sample project for Swift in all the classes that inherit from the Objective-C class TyphoonAssembly all the methods relatives to inject dependencies have the dynamic modifier included in the following way :

public dynamic func weatherReportDao() -> AnyObject {
    return TyphoonDefinition.withClass(WeatherReportDaoFileSystemImpl.self)
}

I thought that I'm missing something, but I don't understand where is the polymorphic operation (method or function) to call at run time here.

What's is the purpose of the dynamic dispatch here?

like image 599
Victor Sigler Avatar asked Apr 16 '15 21:04

Victor Sigler


People also ask

What is dynamic function in swift?

Swift and Objective-C Interoperability Dynamic what? Dynamic dispatch. It simply means that the Objective-C runtime decides at runtime which implementation of a particular method or function it needs to invoke.

What is Dynamic VAR in swift?

@objc exposes the variable to the ObjC runtime. dynamic tells the runtime to use dynamic dispatch instead of the default static dispatch. dynamic also implies @objc so @objc dynamic is redundant. You mostly utilize them in KVO and Cocoa Binding. See this article: krakendev.io/blog/hipster-swift#dynamic.

What is @objc dynamic in swift?

Swift generates code that is only readable to other Swift code. But if we need to interact with the Objective-C runtime, we need to instruct Swift what to do. Here comes @objc to make swift code available to Objective-C.

What is the dynamic keyword used for?

The dynamic keyword is used to declare dynamic types. The dynamic types tell the compiler that the object is defined as dynamic and skip type-checking at compiler time; delay type-checking until runtime. All syntaxes are checked and errors are thrown at runtime.


1 Answers

The answer to your question is addressed in this post:

https://github.com/appsquickly/typhoon/wiki/TyphoonAssembly

Basically at runtime the Typhoon Framework is going to replace your method with its own routine that implements the features of the framework and calls your method to do whatever work you've defined for it.

In order for the framework to be able to replace the method, the method must be dynamically dispatched.

like image 152
Scott Thompson Avatar answered Sep 18 '22 16:09

Scott Thompson