NSURLRequest swift getting fatal error exception

937 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 answers

0
iDhaval 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
Jay Bhalani On

try this one :

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