【问题标题】:JavaScriptCore methods with parameters not being exported from class没有从类中导出参数的 JavaScriptCore 方法
【发布时间】:2016-10-19 23:05:50
【问题描述】:

JavaScriptCore 遇到了一些奇怪的问题。每当我向具有参数的类添加方法时,它似乎都没有被导出(或者以奇怪的名称导出)。例如:

import Cocoa
import JavaScriptCore

@objc
public protocol MyObjectExport: JSExport {
    var property: Int { get set }
    init()
    func test() -> String
    func doNothing(num: Int)
    func squared(num: Int) -> Int
    func sum(a:Int, _ b: Int) -> Int
}

@objc
public class MyObject: NSObject, MyObjectExport {
    public var property: Int {
        get {
            return 5
        }
        set {
            print("New value \(newValue)")
        }
    }

    public required override init() {

    }

    public func test() -> String {
        return "Tested"
    }

    public func doNothing(num: Int) {

    }

    public func squared(num: Int) -> Int {
        return num * num
    }

    public func sum(a: Int, _ b: Int) -> Int {
        return a + b
    }
}

class ViewController: NSViewController {

    override func viewDidLoad() {
        let context = JSContext()!

        context.exceptionHandler = { context, exc in
            print("Exception \(exc)")
        }

        context.setObject(MyObject.self, forKeyedSubscript: NSString(string: "MyObject"))
        context.evaluateScript("var obj = new MyObject()")
        print(context.evaluateScript("obj"))
        print(context.evaluateScript("obj.test()"))
        print(context.evaluateScript("obj.doNothing(5)"))
        print(context.evaluateScript("obj.squared(5)"))
        print(context.evaluateScript("obj.sum(5,5)"))

    }
}

请注意,这必须在 macOS 应用程序中进行测试。 JavaScriptCore 在操场上的行为更加怪异。

运行时,我得到输出

<TestProject.MyObject: 0x608000001180>
Tested
Exception Optional(TypeError: obj.doNothing is not a function. (In 'obj.doNothing(5)', 'obj.doNothing' is undefined))
undefined
Exception Optional(TypeError: obj.squared is not a function. (In 'obj.squared(5)', 'obj.squared' is undefined))
undefined
Exception Optional(TypeError: obj.sum is not a function. (In 'obj.sum(5,5)', 'obj.sum' is undefined))
undefined

如您所见,启动器有效,方法test 也有效。但是,所有其他带参数的方法似乎都没有导出,或者它们以我找不到的其他方法名称导出。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript swift macos javascriptcore


    【解决方案1】:

    从 Swift 3 开始,第一个参数默认不再是未命名的。

    您不能像这样从 Swift 代码调用 doNothing、squared 和 sum:

    doNothing(5)
    squared(5)
    sum(5,5)
    

    您必须包含参数名称:

    doNothing(num: 5)
    squared(num: 5)
    sum(a: 5,5)    //argument name not required for 'b' because it is '_'
    

    这些方法获得 Objective-C 选择器 doNothingWithNum:、squaredWithNum: 和 sumWithA::。根据the documentation for JSExport,这些将按照以下规则导出到 JavaScript:

    当导出带有一个或多个参数的选择器时,JavaScriptCore 会使用以下转换生成相应的函数名称:

    • 从选择器中删除所有冒号。

    • 任何跟在冒号后面的小写字母都大写。

    所以doNothing、squared 和sum 被称为doNothingWithNum()、squaredWithNum() 和sumWithA()。您必须更改 JavaScript 以使用这些名称调用方法:

    print(context.evaluateScript("obj.doNothingWithNum(5)"))
    print(context.evaluateScript("obj.squaredWithNum(5)"))
    print(context.evaluateScript("obj.sumWithA(5,5)"))
    

    或更改您的类和协议定义以删除参数名称:

    @objc
    public protocol MyObjectExport: JSExport {
        var property: Int { get set }
        init()
        func test() -> String
        func doNothing(_ num: Int)
        func squared(_ num: Int) -> Int
        func sum(_ a: Int, _ b: Int) -> Int
    }
    
    @objc
    public class MyObject: NSObject, MyObjectExport {
        public var property: Int {
            get {
                return 5
            }
            set {
                print("New value \(newValue)")
            }
        }
    
        public required override init() {
    
        }
    
        public func test() -> String {
            return "Tested"
        }
    
        public func doNothing(_ num: Int) {
    
        }
    
        public func squared(_ num: Int) -> Int {
            return num * num
        }
    
        public func sum(_ a: Int, _ b: Int) -> Int {
            return a + b
        }
    }
    

    【讨论】:

      【解决方案2】:

      斯威夫特 3

      ///创建协议,继承JSExport

      @objc protocol MYPJSExport: JSExport {
          var sum: Int {get set}
      
          func doNothing()
      
          func squared(_ num: Int) -> Int
          func add(_ a: Int, _ b: Int) -> Int
      
          func add(num: Int) -> Int
          func add(num1: Int, num2: Int) -> Int
          func add(num1: Int, _ num2: Int) -> Int
      //    func add(_ num1: Int, num2: Int) -> Int //the first external parameter is omitted,JS can't call
      }
      

      //注意:@objc 不能丢失

      @objc class MYPObject: NSObject, MYPJSExport {
          var sum: Int = 0 {
              willSet{
                  print("newValue: \(newValue)  |CurrentThread: \(Thread.current)")
              }
              didSet{
                  print("oldValue: \(oldValue)  |CurrentThread: \(Thread.current)")
              }
          }
      
          func add(_ a: Int, _ b: Int) -> Int {
              return a + b
          }
          func doNothing(){
              print("doNothing--")
          }
          func squared(_ num: Int) -> Int {
              return num * num
          }
      
          func add(num: Int) -> Int {
              return num + 10
          }
          func add(num1: Int, num2: Int) -> Int {
              return num1 + num2
          }
          func add(num1: Int, _ num2: Int) -> Int {
              return num1 * num2
          }
      
      
      //    func add(_ num1: Int, num2: Int) -> Int {
      //        return (num1 + num2) * 2
      //    }
      }
      

      ViewController 上的 JS 调用 Swift 方法。

      class ViewController: UIViewController {
      let obj: MYPObject = MYPObject()
      let context:JSContext = JSContext()
      
      override func viewDidLoad() {
          super.viewDidLoad()
      
          context.exceptionHandler = { (context, exception) in
              guard let exce = exception else { return }
              context!.exception = exce
              print("JS exception: \(exce)")
          }
          let block: @convention(block) () -> () = {
              print("++++++Begin Log++++++")
              let args = JSContext.currentArguments()
              for jsVal in args! {
                  print(jsVal)
              }
              print("---End Log------")
          }
          context.setObject(block, forKeyedSubscript: NSString(string: "log"))
          context.setObject(obj, forKeyedSubscript: NSString(string: "Swiftobj"))
      
      
          print(context.evaluateScript("log(Swiftobj.doNothing(5))"))
          print(context.evaluateScript("log(Swiftobj.squared(5))"))
          print(context.evaluateScript("log(Swiftobj.add(5,5))"))
          print(context.evaluateScript("log(Swiftobj.addWithNum(5))"))
          print(context.evaluateScript("log(Swiftobj.addWithNum1Num2(10,10))"))
          print(context.evaluateScript("log(Swiftobj.addWithNum1(10,10))"))
      //        print(context.evaluateScript("log(Swiftobj.addWithNum2(10,10))")) // 'Swiftobj.addWithNum2' is undefined
      
          context.evaluateScript("Swiftobj.sum = Swiftobj.add(2,3)")
          print(context.evaluateScript("log(Swiftobj.sum)"))
          print("obj.sum: \(obj.sum)")
      }
      

      }

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      相关资源
      最近更新 更多