Re: Correct way to specify anchors with NSURL?
Re: Correct way to specify anchors with NSURL?
- Subject: Re: Correct way to specify anchors with NSURL?
- From: Mike Abdullah <email@hidden>
- Date: Thu, 21 Apr 2016 09:23:18 +0200
> On 21 Apr 2016, at 09:09, Graham Cox <email@hidden> wrote:
>
>
>> On 21 Apr 2016, at 3:22 PM, Quincey Morris <email@hidden> wrote:
>>
>> On Apr 20, 2016, at 22:16 , Graham Cox <email@hidden> wrote:
>>>
>>> But it’s 10.9+ only. I really need a solution that will work back to 10.7 (or 10.8 at the latest).
>>
>> Then I’d guess you’d need to construct the entire URL as a string first, then use initWithString. Presumably ‘URLByAppendingPathComponent’ %-encodes a # because you’ve told it that your substring is a *path* component. If you give it a whole string, I’d assume it figures out that it’s got a “fragment” at the end.
>>
>> But I’m just guessing now.
>>
>
>
> Yah, that works. Thanks.
>
> It strikes me that it’s a bug that URLByAppendingPathComponent: doesn’t parse for a fragment just as -initWithString: does though, the anchor/fragment is a perfectly reasonable part of a path and what other possible ambiguious meaning could it have? It makes hard work of something that should be easy.
Nope, very definitely not a bug. The easiest way to do what the OP wants on 10.7 is probably:
NSURL *pathURL = [myURL URLByAppendingPathComponent:@“page”];
NSURL *url = [NSURL URLWithString:@“#fragment” relativeToURL:pathURL];
Don’t forget to properly escape the fragment string if it’s coming in from outside.
Alternatively, grab my KSURLComponents class which clones the NSURLComponents API back to 10.6.
Mike.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden