Convert String to NSURL is return nil in swift

Dharmesh Kheni

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?

Dharmesh Kheni

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.latitud‌​e),\(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.latitud‌​e),\(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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

String is not convert to NSURL [SWIFT]

From Dev

How to convert NSURL to String in Swift

From Dev

Swift 2.0 - "Cannot convert return expression of type 'NSURL' to return type 'String' "

From Dev

NSURL not convert safe string

From Dev

How to convert NSURL to String

From Dev

Unable to convert a string to a NSURL

From Dev

Swift: Nil is incompatible with return type String

From Dev

How to convert NSURL to CFURL in swift?

From Dev

NSURL always returning nil Swift 2.0

From Dev

Swift NSURL nil when running the application

From Dev

Convert string to NSURL with character '\\' inside

From Dev

'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'

From Dev

Swift Delegate return nil

From Dev

swift sequenceType return nil

From Dev

Swift Delegate return nil

From Dev

Compare NSURL String and simple Swift String

From Dev

Cannot convert value of type 'String' to 'NSURL'

From Dev

WhatsApp NSURL returning nil When sharing text - Swift

From Dev

WhatsApp NSURL returning nil When sharing text - Swift

From Dev

Swift | assigning nil to string

From Dev

Compare to nil and return a bool in Swift

From Dev

'queryForTable' cannot return 'nil' In Swift

From Dev

iOS: dateformatter in swift return nil

From Dev

'queryForTable' cannot return 'nil' In Swift

From Dev

How to convert NSNull to nil in Swift?

From Dev

+[NSURL URLWithString:] returning nil

From Dev

+[NSURL URLWithString:] returning nil

From Dev

NSURL/NSURLRequest is unexpectedly nil

From Java

How to Return Nil String in Go?