dynamic
Apply this modifier to any member of a class that can is represented by objective-c. When your mark a member declaration dynamic
with the modifier, access to that member are always dynamically dispatched using T He objective-c runtime. Access to that member was never inlined or devirtualized by the compiler.
Because declarations marked dynamic
with the modifier is dispatched using the OBJECTIVE-C runtime, they must is marked with The objc
attribute.
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ declarations.html#//apple_ref/doc/uid/tp40014097-ch34-id351
But this is just the beginning of the story. Objective-c and Swift use two completely different mechanisms at the bottom, and the Objective-c object in Cocoa is run-time based, and it follows KVC (Key-value Coding, storing object information in a dictionary-like way) and dynamic Dispatch, which determines the specific implementation of the actual invocation when the call is run. And Swift, in pursuit of performance, will not be determined at runtime if there is no special need. In other words, a member or method of the Swift type is determined at compile time, and the runtime no longer needs to go through a lookup and can be used directly.
http://swifter.tips/objc-dynamic/
We can also use KVO in Swift, but only in NSObject
subclasses. This is understandable, because KVO is based on KVC (Key-value Coding) and dynamic dispatch technology, and these things are the concept of objective-c runtime. In addition, because Swift is disabling dynamic distribution by default for efficiency, we want to use Swift to implement KVO, and we need to do the extra work of marking the objects we want to observe dynamic
.
In Swift, the NSObject
simplest example of implementing KVO for a subclass looks like this:
Dynamic keywords
If you have experience with OC development, it will certainly be familiar with the @dynamic keyword in OC, which tells the compiler not to synthesize getter and setter methods for attributes.
Swift also has the dynamic keyword, which can be used to modify a variable or function, and it also has a completely different meaning from OC. It tells the compiler to use dynamic distribution instead of static distribution. One of the characteristics that OC distinguishes from other languages is its dynamic nature, in which any method invocation is actually a message distribution, while Swift is as static as possible.
Therefore, variables/functions marked as dynamic are implicitly prefixed with the @objc keyword, which uses the OC Runtime mechanism.
While static distribution may be better in efficiency, some app analytics statistics libraries rely on dynamically distributed features to dynamically add statistical code that is difficult to achieve under Swift's static distribution mechanism. In this case, although using the dynamic keyword would sacrifice some of the performance optimizations obtained from using static distribution, it is still worthwhile.
With dynamic distribution, you can better interact with some of the features of runtime in OC (such as COREDATA,KVC/KVO), but if you are not sure that variables or functions are dynamically modified, added, or used Method-swizzle, Then you should not use the dynamic keyword, or the program might crash.
54176816
Swift Dynamic Keywords