不从类导出参数的JavaScriptCore方法
我在不从类导出参数的JavaScriptCore方法,javascript,swift,macos,javascriptcore,Javascript,Swift,Macos,Javascriptcore,我在JavaScriptCore方面遇到了一些奇怪的问题。每当我向类中添加一个具有参数的方法时,它似乎不会被导出(或者是用一个奇怪的名称导出)。例如: import Cocoa import JavaScriptCore @objc public protocol MyObjectExport: JSExport { var property: Int { get set } init() func test() -> String func doNoth
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
测试
可选异常(TypeError:obj.doNothing不是函数。(在“obj.doNothing(5)”中,“obj.doNothing”未定义)
未定义
可选异常(TypeError:obj.squared不是函数。(在“obj.squared(5)”中,“obj.squared”未定义)
未定义
可选异常(TypeError:obj.sum不是函数。(在“obj.sum(5,5)”中,“obj.sum”未定义)
未定义
如您所见,启动器工作,方法测试也工作。但是,所有其他带有参数的方法似乎都没有导出,或者它们是以我找不到的其他方法名称导出的
任何帮助都将不胜感激。自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:
。根据以下规则将其导出为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
}
}
从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:
。根据以下规则将其导出为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
}
}
斯威夫特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
// }
}
JS调用ViewController上的Swift方法。
}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
// }
}
JS调用ViewController上的Swift方法。
};请通过翻译非英语部分,确保你的帖子对其他人尽可能有用。好的,明白了。我已经用英语改了;;请通过翻译非英语部分,确保你的帖子对其他人尽可能有用。好的,明白了。我已经用英语改了;