使用swift创建一个web浏览器并获取错误(源文件中的编辑器占位符(,如果用户添加http或仅添加www或仅添加一个单词以在google.com中开始搜索,则搜索栏应该能够获取。
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
let urlString = mySearchBar.text
if urlString?.starts(with: "http://") ?? <#default value#> || ((urlString?.starts(with: "https://")) != nil){
loadUrl(urlString!)
} else if (urlString?.contains("www"))!{
loadUrl("http://(urlString!)")
}else {
searchTextOnGoogle(urlString!)
}
}
func loadUrl(_ urlString: String) {
guard let url = URL(string: urlString) else { return }
let urlRequest = URLRequest(url: url)
myWebView.loadRequest(urlRequest)
}
func searchTextOnGoogle(_ text: String) {
let textComponents = text.components(separatedBy: " ")
let searchString = textComponents.joined(separator: "+")
guard let url = URL(string: "https://www.google.com/search?q=" + searchString) else { return }
let urlRequest = URLRequest(url: url)
myWebView.loadRequest(urlRequest)
}
您错过了代码中的占位符,错误显示。。<#default value#>
下面的代码我删除了带有空字符串的<#default value#>
。
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
let urlString = mySearchBar.text
if urlString?.starts(with: "http://") ?? "" || ((urlString?.starts(with: "https://")) != nil){
loadUrl(urlString!)
} else if (urlString?.contains("www"))!{
loadUrl("http://(urlString!)")
}else {
searchTextOnGoogle(urlString!)
}
}