我编写应用程序,我需要加载相同的 HTML 页面。比需要采取的元素。
getElementById(\"orderData\")
let webView = UIWebView()
let localfilePath = NSBundle.mainBundle().URLForResource("PrintPage", withExtension: "html")
let myRequest = NSURLRequest(URL: localfilePath!)
webView.loadRequest(myRequest)
print(webView.stringByEvaluatingJavaScriptFromString("document"))
但只要打印文档就给我可选“”。 getElementById(\"orderData\") 也写可选""。
附注我可以看到这个页面,加载没有问题。
------更新 P.S.S Deal not in OptionValue =) 当我尝试使用 JavaScript 时处理空字符串
最佳答案
在 Swift 中,如果你打印一个 Optional
值,那么它会使用 Optional"" 打印,如果你不想使用Optional 打印,那么你可以使用以下代码。
if let docString = webView.stringByEvaluatingJavaScriptFromString("document") {
print(docString)
}
关于swift - stringByEvaluatingJavaScriptFromString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36865552/