• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSURL from non-url NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSURL from non-url NSString


  • Subject: Re: NSURL from non-url NSString
  • From: Koen van der Drift <email@hidden>
  • Date: Sun, 25 May 2014 11:31:07 -0400

Alright, thanks all, I'll have a look at NSDataDetectors.

Happy Sunday,

- Koen.


On May 25, 2014, at 11:29 AM, Mike Abdullah <email@hidden> wrote:

>
> On 25 May 2014, at 16:05, Koen van der Drift <email@hidden> 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 (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


References: 
 >NSURL from non-url NSString (From: Koen van der Drift <email@hidden>)
 >Re: NSURL from non-url NSString (From: Mike Abdullah <email@hidden>)

  • Prev by Date: Re: NSURL from non-url NSString
  • Next by Date: Re: Understanding ARC
  • Previous by thread: Re: NSURL from non-url NSString
  • Next by thread: Re: NSURL from non-url NSString
  • Index(es):
    • Date
    • Thread