问题
I am trying to convert a String
to NSURL
and my code for that is Below:
var url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(self.latitude),\(self.longitude)&destinations=\(self.stringForDistance)&language=en-US"
println("This is String: \(url)")
var remoteUrl : NSURL? = NSURL(string: url)
println("This is URL: \(remoteUrl)")
And console prints something like this:
This is String: https://maps.googleapis.com/maps/api/distancematrix/json?origins=-34.4232722,150.8865837&destinations=-34.4250728,150.89314939999997|-34.4356434,150.8858692|-34.4250728,150.89314939999997|-34.4356434,150.8858692|-34.4250728,150.89314939999997|-34.4356434,150.8858692|-34.423234,150.88658899999996|-34.423234,150.88658899999996|-34.428251,150.899673|-34.4257439,150.89870229999997|-34.423234,150.88658899999996|-34.4257439,150.89870229999997|-34.425376,150.89388299999996&language=en-US
This is URL: nil
The remoteUrl
is nil
and I don't know what is the problem here.
After that I try sort String
like this:
var url : String = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=-34.4232722,150.8865837&destinations=-34.4250728,150.89314939999997&language=en-US"
println("This is String: \(url)")
var remoteUrl : NSURL? = NSURL(string: url)
println("This is URL: \(remoteUrl)")
And console prints:
This is String: https://maps.googleapis.com/maps/api/distancematrix/json?origins=-34.4232722,150.8865837&destinations=-34.4250728,150.89314939999997&language=en-US
This is URL: Optional(https://maps.googleapis.com/maps/api/distancematrix/json?origins=-34.4232722,150.8865837&destinations=-34.4250728,150.89314939999997&language=en-US)
This is working fine.
So can anybody please tell me what is wrong with my first case?
回答1:
As suggested by the Martin R, I see THIS post and I converted that objective-c code to swift and I got this code:
var url : NSString = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(self.latitude),\(self.longitude)&destinations=\(self.stringForDistance)&language=en-US"
var urlStr : NSString = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
var searchURL : NSURL = NSURL(string: urlStr)!
println(searchURL)
and this is working correctly.
For swift 3.0:
let url : NSString = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(self.latitude),\(self.longitude)&destinations=\(self.stringForDistance)&language=en-US"
let urlStr : NSString = url.addingPercentEscapes(using: String.Encoding.utf8.rawValue)! as NSString
let searchURL : NSURL = NSURL(string: urlStr as String)!
print(searchURL)
回答2:
as blwinters said, in Swift 3.0 use
URL(string: urlPath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
回答3:
I think try this it's perfectly work for me
var url : String = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=-34.4232722,150.8865837&destinations=-34.4250728,150.89314939999997&language=en-US"
println("This is String: \(url)")
var urlStr : NSString = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
var remoteUrl : NSURL? = NSURL(string: url)
println("This is URL: \(remoteUrl!)")
回答4:
You can get following error if NSURL is null and trying load http URL over web view:
fatal error: unexpectedly found nil while unwrapping an Optional value
To be safe we should use:
var urlStr = strLink!.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
if var url = NSURL(string: urlStr!){
println(self.strLink!)
self.webView!.loadRequest(NSURLRequest(URL: url))
}
回答5:
SWIFT 3.0
A safe way to fix a bad string being converted to NSURL is by unwrapping the urlPath string variable using "guard let"
guard let url = NSURL(string: urlPath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
else
{
print("Couldn't parse myURL = \(urlPath)")
return
}
The variable called "urlPath" in my above example would be the url string you have already declared somewhere else in your code.
I came across this answer because randomly I was getting the nil error with XCode breaking at the point my string was made into a NSURL. No logic as to why it was random even when I printed the url's they would look fine. As soon as I added the .addingPercentEncoding it was back working without any issues whatsoever.
tl;dr For anyone reading this, try my above code and swap out "urlPath" for your own local string url.
回答6:
this work for me
let url : NSString = MyUrls.baseUrl + self.url_file_open as NSString
let urlStr : NSString = url.addingPercentEscapes(using: String.Encoding.utf8.rawValue)! as NSString
if let url = URL(string: urlStr as String) {
let request = URLRequest(url: url)
self.businessPlanView.loadRequest(request)
}
来源:https://stackoverflow.com/questions/28514622/convert-string-to-nsurl-is-return-nil-in-swift