2016-11-16 76 views
-2

在Swift 2.3中运行此代码时,它很好。当我开始更新Swift 3的代码时,问题就开始了。我NSURL返回零,当我使用下面的代码:NSURL以Swift 3.0返回零。

{ 

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了很多,但无法找到答案。

回答

1

在斯威夫特有很多语法的变化,你可以让你的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

+0

感谢Ajay.it工作。 –