【发布时间】:2017-03-02 17:53:16
【问题描述】:
我目前正在用 Swift 编写一个与比特币相关的应用程序。由于没有适用于 BIP32/39 的本机库,我决定使用 JavaScriptCore 使用 js 库。
问题是,在 Swift 中几乎所有内容都输出 undefined,例如:
var pip_buffer = new Uint8Array(strength / 8);
这个有效:
var pip_buffer = "Hello, world"
这是我的 Swift 代码:
var context: JSContext?
private func initJS() {
context = JSContext()
if let jsSrcPath = Bundle.main.path(forResource: "script", ofType: "js") {
do {
let jsSrcContents = try String(contentsOfFile: jsSrcPath)
_ = context?.evaluateScript(jsSrcContents)
} catch let error {
print(error.localizedDescription)
}
}
}
private func getJSVar(name: String) {
if let vb = context?.objectForKeyedSubscript(name) {
print("\(vb)")
}
}
override func viewDidLoad() {
super.viewDidLoad()
initJS()
getJSVar(name: "pip_buffer")
}
如何让这件事发挥作用?
【问题讨论】:
标签: ios swift cocoa-touch javascriptcore