Jspatch meets Swift

Source: Internet
Author: User
Tags gcd

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
    • JSPatchThe 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.