[英]iOS 8 JavaScriptCore: Where do you place your JavaScript files at?
How do you add a JS file to the home directory so that 's' does not return 'nil'. 如何将JS文件添加到主目录,以使“ s”不返回“ nil”。 Thanks.
谢谢。
func analyzeText(text: String) {
let homeDir = NSHomeDirectory()
var err: NSError? = NSError()
let s = String(contentsOfFile: homeDir + "/text.js", encoding: NSUTF8StringEncoding, error: &err)
println(s)
var context = JSContext(virtualMachine: JSVirtualMachine())
context.evaluateScript(s)
let analyzeText = context.objectForKeyedSubscript("analyzeText")
let analyzeTextVal = analyzeText.callWithArguments([text])
println(analyzeTextVal)
}
The following code works for me: 以下代码对我有用:
func analyzeText(scriptName: String) {
var ext = scriptName.pathExtension
var fileName = scriptName.substringToIndex(
advance(scriptName.startIndex,
scriptName.utf16Count - ext.utf16Count - 1))
if fileExtension.utf16Count == 0 {
fileName = scriptName
ext = "js"
}
let url = NSBundle.mainBundle()
.URLForResource(fileName, withExtension: ext)
let scriptCode = String(contentsOfURL: url!,
encoding: NSUTF8StringEncoding,
error: nil)!
var context = JSContext(virtualMachine: JSVirtualMachine())
context.evaluateScript(scriptCode)
/* ... */
}
To evaluate a file called text.js
, do the following: 要评估名为
text.js
的文件,请执行以下操作:
analyzeText("text")
Be sure to have added text.js
to the Xcode project copying the file if necessary to the destination 确保已将
text.js
添加到Xcode项目中,如有必要,将文件复制到目标位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.