没有从类

时间:2016-10-19 23:05:50

标签: javascript swift macos javascriptcore

我遇到了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也起作用。但是,所有其他带参数的方法似乎都没有导出,或者它们是以我找不到的其他方法名称导出的。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

自Swift 3起,默认情况下第一个参数不再未命名。

您无法像这样从Swift代码中调用doNothingsquaredsum

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::。根据{{​​3}}:

,这些规则将根据以下规则导出到JavaScript
  

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

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

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

  •   

因此doNothingsquaredsum被称为doNothingWithNum()squaredWithNum()sumWithA()。您必须更改JavaScript以使用这些名称调用方法:

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

或者改变你的课程&amp;用于删除参数名称的协议定义:

@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
    }
}

答案 1 :(得分:0)

Swift 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)")
}

}