-2

This is my code. Error: fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) I have no idea where;s the problem

res = "https://www.example.com/range.php?arr1=48.15&arr2=48.15&API_KEY=>code"
    let url = NSURL(string: res)! // error line
    print("url craeted" + res)
    return url
  • 5
    `>` is not a valid character for an URL. Encode your URL: http://stackoverflow.com/q/24551816/2227743, http://stackoverflow.com/a/30149081/2227743, etc – Eric Aya Feb 19 '16 at 15:21

2 Answers2

0

You can use an extension to encode your URL

extension String {
    func urlEncodedString() -> String {
        let urlEncodedString = self.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
        return urlEncodedString ?? self
    }
}
Vinícius Albino
  • 527
  • 1
  • 7
  • 23
0

The combination => leads to an instruction failure and let's the string not be interpreted as a string. By the way, I assume res has been defined somewhere else in your code and you want to change "url created" into "url created", later.

MacUserT
  • 1,760
  • 2
  • 18
  • 30