• 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: Properly comparing file NSURLs for equality?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Properly comparing file NSURLs for equality?


  • Subject: Re: Properly comparing file NSURLs for equality?
  • From: Mike Abdullah <email@hidden>
  • Date: Sat, 28 May 2011 01:06:31 +0100

It is a bit of a knotty problem. I've ended up with:

- (BOOL)ks_isEqualToURL:(NSURL *)otherURL;
{
    BOOL result = [self isEqual:otherURL];

   // For file: URLs the default check might have failed because they reference the host differently. If so, fall back to checking paths
    if (!result && [self isFileURL] && [otherURL isFileURL])
    {
        result = [[self path] isEqualToString:[otherURL path]];
    }

    return result;
}

From here: https://github.com/karelia/KSFileUtilities/blob/master/KSURLUtilities.m

Part of the problem really is expressing what equality you actually want to test. For example, it's perfectly legal for a file URL to have a query or fragment. But the filesystem will ignore that, rendering it obsolete for a URL comparison. Thus the only safe way to test equality as you want here is to compare paths.

On 28 May 2011, at 00:13, Sean McBride wrote:

> Hi all,
>
> What is the correct way to test if two NSURLs refer to the same file
> system object?
>
> I've just learnt the hard way that one can have two URLs that refer to
> the same file system object but because they are subtly different,
> isEqual: returns NO:
>
> ex:
>
> <file://localhost/Volumes/Disk/Users>
> vs
> <file:///Volumes/Disk/Users>
>
> isEqual: returns NO.  I guess this makes some sense, but it's not the
> kind of equality I'm looking for.
>
> What would best practice be?  Convert to full path, and compare those?
> Note: I'm not concerned about resolving symlinks or aliases.
>
> (Aside: I was surprised to see that there is no isEqualToURL:, analogous
> to isEqualToString, isEqualToSet, etc.  I wonder why...)
>
> Thanks,
>
> --
> ____________________________________________________________
> Sean McBride, B. Eng                 email@hidden
> Rogue Research                        www.rogue-research.com
> Mac Software Developer              Montréal, Québec, Canada
>
>
> _______________________________________________
>
> 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

_______________________________________________

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

  • Follow-Ups:
    • Re: Properly comparing file NSURLs for equality?
      • From: Lee Ann Rucker <email@hidden>
References: 
 >Properly comparing file NSURLs for equality? (From: Sean McBride <email@hidden>)

  • Prev by Date: Re: Properly comparing file NSURLs for equality?
  • Next by Date: Re: Properly comparing file NSURLs for equality?
  • Previous by thread: Re: Properly comparing file NSURLs for equality?
  • Next by thread: Re: Properly comparing file NSURLs for equality?
  • Index(es):
    • Date
    • Thread