nazaniel nazaniel - 7 months ago 23
Swift Question

NSURL relativeToURL backwards?

My NSURLs are being formatted backwards, even when copying and pasting in code from other projects which makes zero sense.

For example:

let baseURL = NSURL(string: "http://example.com/v1/")
NSURL(string: "foo", relativeToURL: baseURL)


Should return: "http://example.com/v1/foo"

Instead returns: "foo -- ttp://example.com/v1/"

Has anyone seen this before?

Answer

That's simply how relative URLs are displayed in their (debug) description. The absoluteString will still be correct; this format is unrelated.

let baseURL = NSURL(string: "http://example.com/v1/")!
let u = NSURL(string: "foo", relativeToURL: baseURL)!

print(u)  // prints the URL's description, "foo -- http://example.com/v1/"
print(u.absoluteString)  // prints "http://example.com/v1/foo"
Comments