JavascriptCore 不从类中导出方法
JavascriptCore not exporting methods from clases
我正在尝试从 swift class.
的单例实例调用方法
这是我的协议:
@objc public protocol MyProtocol: JSExport {
var name: String { get set }
func sum(a:Int, b: Int) -> Int
}
这是我的 class:
private let sharedInstance = MySingleton()
@objc public class MySingleton: NSObject, MyProtocol {
// Singleton
public class var sharedApp : MySingleton {
return sharedInstance
}
public var username: String
override private init() {
self.username = "Oscar"
super.init()
}
public func sum(a: Int, b: Int) -> Int {
return a + b
}
}
我运行一些测试。当我访问 属性 时,它会通过:
func testName() {
var virtualMachine = JSVirtualMachine()
js = JSContext(virtualMachine: virtualMachine)
var singleton: MySingleton = MySingleton.sharedApp
self.context.setObject(
singleton.self,
forKeyedSubscript: "Singleton")
let name = js.evaluateScript("Singleton.username").toString()
XCTAssertTrue(name == "Oscar", "Name is not Colossal, was " + name); // PASS!!!!!
}
但是,当我调用一个方法时它失败了:
func testSum() {
var virtualMachine = JSVirtualMachine()
js = JSContext(virtualMachine: virtualMachine)
var singleton: MySingleton = MySingleton.sharedApp
self.context.setObject(
singleton.self,
forKeyedSubscript: "Singleton")
let name = js.evaluateScript("Singleton.sum(3, 2);").toInt32()
XCTAssertTrue(sum == 5, "Sum was " + String(sum)); // Fails. Says sum was 0
}
它仅在 reading/writing 属性 from/to Javascript 对象时有效,在调用方法时无效....
我做错了什么?
我刚刚找到了解决方案。试试这个。
func sum(a:Int, _ /* <-- make this argument unnamed */ b: Int) -> Int
好像JSExport
还不支持命名参数。
JSExport 支持命名参数,你只需要将它们添加到函数名称中,例如:
swift 函数:
func myFunction(param1 : String, param2 : String, param3 : String)
必须从 javascript 调用:
myFunctionParam2Param3( "a", "b", "c")
在你的情况下,只需调用 sumB() 而不是 sum():
let name = js.evaluateScript("Singleton.sumB(3, 2);").toInt32()
我正在尝试从 swift class.
的单例实例调用方法这是我的协议:
@objc public protocol MyProtocol: JSExport {
var name: String { get set }
func sum(a:Int, b: Int) -> Int
}
这是我的 class:
private let sharedInstance = MySingleton()
@objc public class MySingleton: NSObject, MyProtocol {
// Singleton
public class var sharedApp : MySingleton {
return sharedInstance
}
public var username: String
override private init() {
self.username = "Oscar"
super.init()
}
public func sum(a: Int, b: Int) -> Int {
return a + b
}
}
我运行一些测试。当我访问 属性 时,它会通过:
func testName() {
var virtualMachine = JSVirtualMachine()
js = JSContext(virtualMachine: virtualMachine)
var singleton: MySingleton = MySingleton.sharedApp
self.context.setObject(
singleton.self,
forKeyedSubscript: "Singleton")
let name = js.evaluateScript("Singleton.username").toString()
XCTAssertTrue(name == "Oscar", "Name is not Colossal, was " + name); // PASS!!!!!
}
但是,当我调用一个方法时它失败了:
func testSum() {
var virtualMachine = JSVirtualMachine()
js = JSContext(virtualMachine: virtualMachine)
var singleton: MySingleton = MySingleton.sharedApp
self.context.setObject(
singleton.self,
forKeyedSubscript: "Singleton")
let name = js.evaluateScript("Singleton.sum(3, 2);").toInt32()
XCTAssertTrue(sum == 5, "Sum was " + String(sum)); // Fails. Says sum was 0
}
它仅在 reading/writing 属性 from/to Javascript 对象时有效,在调用方法时无效....
我做错了什么?
我刚刚找到了解决方案。试试这个。
func sum(a:Int, _ /* <-- make this argument unnamed */ b: Int) -> Int
好像JSExport
还不支持命名参数。
JSExport 支持命名参数,你只需要将它们添加到函数名称中,例如:
swift 函数:
func myFunction(param1 : String, param2 : String, param3 : String)
必须从 javascript 调用:
myFunctionParam2Param3( "a", "b", "c")
在你的情况下,只需调用 sumB() 而不是 sum():
let name = js.evaluateScript("Singleton.sumB(3, 2);").toInt32()