NSURL 返回 nil Swift 3.0

NSURL returning nil with Swift 3.0

虽然 运行 此代码带有 Swift 2.3,但没问题。当我开始为 Swift 3 更新我的代码时,问题开始了。当我使用以下代码时,我的 NSURL returns 为零:

{

var searchResults: [String]!

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let correctedAddress = self.searchResults[indexPath.row].addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)        
let urlpath:NSString! = "https://maps.googleapis.com/maps/api/geocode/json?address=\(correctedAddress)" as NSString!
let url = NSURL.init(string: urlpath as String)

}

 }           

我是 iOS 编程的新手,所以我的问题可能非常基础。我用 Google 搜索了很多,但找不到答案。

在 Swift 中,语法有很多变化,您可以像这样制作 URL:-

let correctedAddress = "Delhi " //You can replace your address here
let urlpath = "https://maps.googleapis.com/maps/api/geocode/json?address=\(correctedAddress)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let url = URL(string: urlpath!)
print(url!)

结果:- https://maps.googleapis.com/maps/api/geocode/json?address=Delhi%20