URL Parsing in Cocoa

  • http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_cl
    assic/Classes/NSURL.html#//apple_ref/doc/uid/20000301/BAJBBDIB


    I'm stumped.  The documentation for NSURL+URLWithString says that if the
    string is not well-formed (per RFC 2396) it should return nil.  However, I
    am not seeing this at all on 10.2.8.  Specifically,

    ([NSURL URLWithString:@"google.com"] == nil)

    evaluates to false. This seems like a bug to me, since well-formed URLs
    should have a scheme, like "http://www.google.com".  I suppose
    "google.com" could parse as a relative URL, but in that case I would be
    using NSURL+URLWithString:relativeToURL.

    Is this a bug? Is there another way to parse a URL in Cocoa that will
    actually let me know if the URL is valid?

    Nick Matsakis
  • The problem is that "google.com" is, in fact, a valid URL; it's just
    not a valid absolute URL.  Invalid URLs have non-ascii characters in
    them, or violate a few of the other specific parsing rules.  In this
    case, "google.com" is (I think) being interpreted by NSURL as a
    relative path with no base URL given.

    If you want to know whether you have a fully-formed, absolute URL, try
    looking at [[url absoluteURL] scheme]; if this is nil, then you don't
    have a well-formed, absolute URL.

    Hope that helps,
    REW

    On Nov 10, 2003, at 3:33 PM, Nick Matsakis wrote:

    >
    > http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
    > ObjC_classic/Classes/NSURL.html#//apple_ref/doc/uid/20000301/BAJBBDIB
    >
    > I'm stumped.  The documentation for NSURL+URLWithString says that if
    > the
    > string is not well-formed (per RFC 2396) it should return nil.
    > However, I
    > am not seeing this at all on 10.2.8.  Specifically,
    >
    > ([NSURL URLWithString:@"google.com"] == nil)
    >
    > evaluates to false. This seems like a bug to me, since well-formed URLs
    > should have a scheme, like "http://www.google.com".  I suppose
    > "google.com" could parse as a relative URL, but in that case I would be
    > using NSURL+URLWithString:relativeToURL.
    >
    > Is this a bug? Is there another way to parse a URL in Cocoa that will
    > actually let me know if the URL is valid?
    >
    > Nick Matsakis
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
previous month november 2003 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today
MindNode
MindNode offered a free license !