Hi, I'm using a database to get some values, and i don't understand why the NSURL return nil. Here the code that works :
let urlString = "https://api.mlab.com/api/1/databases/lampadaire/collections/lampadaire?&apiKey=0HW9bYZVJEvmmV93Yo8jYADX3gWczJie"
print(urlString) // this return https://api.mlab.com/api/1/databases/lampadaire/collections/lampadaire?&apiKey=0HW9bYZVJEvmmV93Yo8jYADX3gWczJie
print(NSURL(string: urlString)) // return a thing
You can try the URL in the adress bar, it returns JSON, but when i tried to put a query inside, it doesn't work :
rlString = "https://api.mlab.com/api/1/databases/lampadaire/collections/lampadaire?q={\"lampadaire_id\":1}&apiKey=0HW9bYZVJEvmmV93Yo8jYADX3gWczJie"
print(urlString) // this return https://api.mlab.com/api/1/databases/lampadaire/collections/lampadaire?q={"lampadaire_id":1}&apiKey=0HW9bYZVJEvmmV93Yo8jYADX3gWczJie
print(NSURL(string: urlString)) // return nil
You can try the URL in the adress bar, it returns JSON too. I think it's because of the double quote in the query, how can I pass it please ?
You probably need to "escape" the string using % encoding. The old (deprecated but still allowed) is to use stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEndocding). The replacement is stringByAddingPercentEncodingWithAllowedCharacters(...). With the new way, there are predifined allowed character sets for each part of the url, such as NSCharacterSet.URLHostAllowedCharacterSet() and NSCharacterSet.URLQueryAllowedCharacterSet()