gpt4 book ai didi

带有未从类导出参数的 JavaScriptCore 方法

转载 作者:搜寻专家 更新时间:2023-11-01 06:37:11 24 4
gpt4 key购买 nike

我在使用 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 在 Playground 上的表现甚至更奇怪。

运行时,我得到了输出

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

如有任何帮助,我们将不胜感激。

最佳答案

从 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::。根据 the documentation for JSExport,这些将按照以下规则导出到 JavaScript :

When exporting a selector that takes one or more arguments, JavaScriptCore generates a corresponding function name using the following conversion:

  • All colons are removed from the selector.

  • Any lowercase letter that had followed a colon is capitalized.

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

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

或者更改类和协议(protocol)定义以删除参数名称:

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

关于带有未从类导出参数的 JavaScriptCore 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40142911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com