Swift uses Jspatch essentials:
- The Swift class inherits from the NSObject, its methods that inherit from the parent class are dynamic, other custom methods and properties need to be modified to be dynamic (except for the public property) example can be see in the project:Demo1.js, Demo1_1.js, Demo2.js
- The pure Swift class has no dynamic nature. This means that methods and properties of the pure Swift class cannot be overridden. Demo6,demo7
JSPatch
The implementation of the overrideMethod
protocol is required when a method is implemented to implement IMP substitution, class
and the NSCoping
Swift class that does not inherit from does NSObject
not follow the protocol and therefore crashes.
Static void _initjpoveridemethods (Class cls) { if (! _jsoveridemethods) { = [[Nsmutabledictionary alloc] init]; } if (! _jsoveridemethods[cls]) { _jsoveridemethods[(ID<NSCopying>) CLS] = [[Nsmutabledictionary alloc] init];} }
Here, when the buffer is initialized, it is JSPatch
Class
Dictionary
key
saved as, and Dictionary
the key-value
value is copied when set, so it key
causes a message to be sent to an object that does not follow NSCoying
the protocol copyWithZone:
. Cause a crash.
- On GitHub, there have been friends to Jspatch's author submitted issue, there is no solution for the moment, I hope Swift 3.0 will be able to have a good solution. Issue Address
- The runtime in Swift is significantly weaker than the runtime in OC; the
objc_msgSend
function cannot be used with Swift object, which causes the JSPatch
underlying mechanism of the implementation method invocation (message forwarding) to fail in Swift, which is also where the hot fix really has no solution. is also the most deadly.
- Similarly, the classes implemented by protocol are not dynamic, unless they inherit from the NSObject , and return to the previous conclusion Demo6
- testing Versatility: Use of block in swift, simple animation of view, GCD support Demo4
- Note: If you use self in the block body, for example, use Var to redefine the variable. Gcd,animation is not required in swift or OC, but it needs to be redefined in JS
- extensions follow the dynamics of their extended classes Demo5
- If the method's parameter/property type is Swift-specific (such as character/tuple), then this method and property cannot be called by JS.
Summarize:
- Only calls to the Swift class that inherit from NSObject are supported, and the dynamic keyword is required to mark properties and methods for dynamics if the method's parameter/property type is Swift-specific (such as character/tuple), it cannot be called by JS
- A multi-class method replaces static methods, making them dynamic
- Try to avoid pure swift classes, using the Swift class that inherits NSObject to make it dynamic
- The above points are for Swift APP
- Demo Address
Related documents:
- Jspatch Platform Introduction
- Jspatch Basic Usage
- Http://www.jianshu.com/p/e2eb7b4861c5
Jspatch meets Swift