2016-11-16 117 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工作。 –