NSURLRequest swift getting fatal error exception

958 Views Asked by At

I am new to swift code.I am getting the following error while i am passing my api to NSMutableRequest.What will be my mistake in this.My code is my urlString is http://185.43.139.198/N********e/mer***se?wt=json&c=Chennai&s=ddrrss

 let urlRequest = NSURLRequest(URL: NSURL(string: urlString)!)

and am getting the log console as

fatal error: unexpectedly found nil while unwrapping an Optional value

2

There are 2 best solutions below

0
On BEST ANSWER

If Your Url Contain Spaces than it will Return Nil

Try This:

let url = NSURL(string: urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
let request = NSURLRequest(URL: url!)
2
On

try this one :

let url = NSURL(string: "http://www.google.com")
let request = NSURLRequest(URL: url!)