On Thu, Aug 29, 2013, at 11:02 AM, Koen van der Drift wrote:
> I'm having some difficulties constructing a URL from a baseURL and a
> relativeURL when the relativeURL starts with a question mark:
> 
>     NSURL *baseURL = [NSURL URLWithString: @"http://www.test.com/test/";];
>     NSString *relativeString = @"?query=test";
> 
>     NSURL *url = [NSURL URLWithString: relativeString relativeToURL:
>     baseURL];
> 
> This results in the following url:
> 
> ?query=test -- http://www.test.com/test/
> 
> While I want it to be:
> 
> http://www.test.com/test/?query=test
> 
> 
> I can just generate the whole URL at once, but I was wondering why I get
> this behavior?

You're looking at the output of -[NSURL description], which remembers
whether the URL was created as an absolute or relative URL and prints
itself accordingly.

If you send -absoluteString to the URL, you'll find that it produces the
correct output:

% python
Python 2.7.2 (default, Oct 11 2012, 20:14:37) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on
darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Foundation
>>> Foundation.NSURL.URLWithString_relativeToURL_('?query=test', 
>>> Foundation.NSURL.URLWithString_('http://www.test.com/test/'))
?query=test -- http://www.test.com/test/
>>> relURL=_
>>> relURL.absoluteString()
u'http://www.test.com/test/?query=test'

--Kyle Sluder
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to