On 25 May 2014, at 16:05, Koen van der Drift <koenvanderdr...@gmail.com> wrote:

> Hi,
> 
> At one point in my code I need to recognize an URL to show in a different 
> color. Pretty standard:
> 
>    NSURL *url = [NSURL URLWithString: aString];
> 
>    if (url)  // if url is created change color
>    {
>        NSAttributedString *linkString = [[NSAttributedString alloc] 
> initWithString: aString
>                                                                         
> attributes: @{NSForegroundColorAttributeName: [UIColor blueColor]}];
> 
>       ...etc
>    }
> 
> Works great, if aString starts with eg http:// it will be in blue; regular 
> strings won't.
> 
> 
> However, I noticed that strings that have the form P12345 also turn blue.  
> Any idea why that string would turn into an NSURL?  The url scheme is nil, 
> and the path components just show P12345.  
> 
> (I could test above for if (url.scheme) to avoid this, but knowing why this 
> happens would actually help me with other parts in my app).

NSURL accepts anything while conforms to the URL specs (as listed at the top of 
the documentation). You probably want the litmus test of if the 
resourceSpecifier begins with a double slash (again, as detailed at the top of 
the docs).

As Uli says though, there’s a pretty good chance if this is user input, you 
want to be more lenient, and go with what NSDataDetector can parse out.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com

Reply via email to