運行時錯誤的原因是喲你打開一個NSURL
的可選實例,實際上是nil
。
的urls
原因是nil
是searchurl
字符串包含invalid字符(外側的7位ASCII範圍的)。在字符應該百分比編碼的URL中使用。
斯威夫特2(我猜你正在使用的版本):
let encodedTexts = texts.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
if let encodedTexts = encodedTexts {
let searchurl = "http://sngpoisk.ru/search-location/?search_keywords=\(encodedTexts)&search_location=&place_location=&latitude=&longitude="
let urls = NSURL(string:searchurl)
if let urls = urls {
let ret = NSURLRequest(URL:urls)
Browser!.loadRequest(ret)
}
}
斯威夫特3:
let encodedTexts = texts.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
if let encodedTexts = encodedTexts {
let searchurl = "http://sngpoisk.ru/search-location/?search_keywords=\(encodedTexts)&search_location=&place_location=&latitude=&longitude="
let urls = URL(string:searchurl)
if let urls = urls {
let ret = URLRequest(url:urls)
Browser!.loadRequest(ret)
}
}