stringByEvaluatingJavaScriptFromString

时间:2016-04-26 12:53:01

标签: swift uiwebview

我写app,我需要加载相同的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。我可以看到这个页面,加载没问题。

------更新 P.S.S交易不在OptionValue =)当我尝试使用JavaScript时处理空字符串

2 个答案:

答案 0 :(得分:0)

在Swift中,如果您打印Optional值,则打印时使用可选“”,如果您不想使用Optional打印,则可以使用以下代码。

if let docString = webView.stringByEvaluatingJavaScriptFromString("document") {
     print(docString)
}

答案 1 :(得分:0)

由于class User(Model): name = CharField(unique=True) def __hash__(self): return hash(self.name) class User(Model): label = CharField(unique=True) def __hash__(self): return hash(self.label) 方法返回可选字符串stringByEvaluatingJavaScriptFromString    它将在输出中打印String?

您需要参考Optional Chaining in swift language

相关问题