Search code examples
iosxcodeswiftnsurlnsurlrequest

NSURLRequest swift getting fatal error exception


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


Solution

  • If Your Url Contain Spaces than it will Return Nil

    Try This:

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