Does Swift have the same runtime mechanism as OC
The most important feature of OC language is its dynamic nature, it can use the dynamic of OC to obtain a class of methods and properties, so as to achieve a flexible program, but does Swift also include the runtime mechanism?
Reference Link: http://mp.weixin.qq.com/s?__biz=MzA3ODg4MDk0Ng==&mid=403153173&idx=1&sn= C631f95b28a0eb4b842a9494e43a30e5&scene=23&srcid=0331zwo8t6uwibon621r1ghc#rd
Below we will describe Swift's runtime mechanism from the pure Swift class and the Swift class inheriting OC
Analysis Use cases:
Method, Property
The most important thing about dynamic is getting the methods and properties of a class and printing the methods and properties of the class using the following method
Call Showclsruntime Printing method
Print as follows:
For pure Swift's Testaswiftclass, no methods, no attributes are obtained.
For Testswiftclass, in addition to the Testreturntuple, Testreturnvoidwithacharacter two methods, the others have been successful.
What is this for?
1: The Pure Swift function call is no longer a run-time message like OC, but is similar to C + + vtable, which is determined at compile time to call that function.
2: While Testswiftclass inherits from Uiviewcontroller that is nsobject,swift in order to be compatible with OC, the class that inherits from NSObject retains his dynamic nature, So we can get his attributes and methods through the runtime.
but why testreturntuple, Testreturnvoidwithacharacter These two functions can not be obtained through runtime?
from the dynamic characteristics of OC, all runtime methods are dependent on typeecoding, i.e. method _gettypeencoding function, stack memory space, without this logo can not enter stack. And the progenitor, and character types are unique to swift, so you can't use runtime to get his way.
Method overrides
The most common method of dynamic is the method substitution, which replaces the method of a class with a custom method, which acts as a hook.
For pure Swift classes (such as Testaswiftclass), it is not possible to replace the method with the OBJC runtime because the above test shows that these methods, properties
For inheriting from the NSObject class (such as TESTSWIFTVC), the method that cannot be obtained by the runtime must not be replaced. Can the method that can be obtained by the runtime be replaced? Let's take a test.
The code for Method swizzling is as follows
@objc
Find official documents to read.
You can know that @objc is used to export the Swift API to Objective-c and OBJECTIVE-C runtime, and if your class inherits from the Objective-c class (such as NSObject), it will automatically be inserted into the @objc identity by the compiler.
We are trying to add a @objc to the Testaswiftclass (Pure Swift Class) method and the properties before
dynamic
There's a note in the document:
Viewdidappear is a method inherited from the Objective-c class, and is itself modified to dynamic, so it can be replaced dynamically.
Does Swift have the same runtime mechanism as OC