Anyone knows why am I getting
fatal error: unexpectedly found nil while unwrapping an Optional value
When i use
let URL = NSURL(string: "https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907")!
Anyone knows why am I getting
fatal error: unexpectedly found nil while unwrapping an Optional value
When i use
let URL = NSURL(string: "https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907")!
The |
character its not a valid URL character so you must replace it with percent escape character. Encoding whole string will do that automatically for you
var stringUrl = "https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907"
let URL = NSURL(string: stringUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)!
Swift 4
I was trying with :
https://api.url.com/method?token=abcdfghijklmopqrst&command=>SSSXP10<&otherParam=12345678
And the '>' and '<' characters was giving me the error.
For the solution :
let objectUrl = URL(string:url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
Oh, It's a crazy issue. Because of < and > it was giving an error
let username = searchTextFileld.text!
let urlString: String = "https://api.github.com/search/users?q=\(username)+repos:>100+followers:>100"
© 2022 - 2024 — McMap. All rights reserved.
stringByAddingPercentEscapesUsingEncoding
is deprecated. UsestringUrl .stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
– Komarek