2

I am solving a bug caused by path comparison using -[NSString isEqualToString:]

(lldb) po aString
/var/mobile/Containers/Bundle/Application/DE14BC26-B07D-46C2-89BF-E800231BAD1F/a.app/b.bundle/epub/OEBPS/Text/02.xhtml

(lldb) po bString
/private/var/mobile/Containers/Bundle/Application/DE14BC26-B07D-46C2-89BF-E800231BAD1F/a.app/b.bundle/epub/OEBPS/Text/02.xhtml

[aString isEqualToString:bString] would yield NO.

But since /var is a symlink to /private/var, they are referring to the same file. May I know the correct way to fix this?

jscs
  • 63,694
  • 13
  • 151
  • 195
chubao
  • 5,871
  • 6
  • 39
  • 64
  • One starts with `/private/var` and the other is `/var`, it's not the same even by looking – Cai Mar 10 '16 at 01:23
  • var is symlink to private/var – chubao Mar 10 '16 at 01:26
  • I know, but remember they are `NSString` objects, so they were compared as strings and as objects they were not the same. – Cai Mar 10 '16 at 01:27
  • I added an answer to resolve the symlink from `NSString`. – Cai Mar 10 '16 at 01:33
  • I found a very good article related to NSURL file comparison: https://digitalleaves.com/blog/2013/09/nsurl-files-equality-comparison-and-equivalency/ – chubao Mar 10 '16 at 02:06

1 Answers1

3

Try using:

NSString *resolvedPath = [path stringByResolvingSymlinksInPath];

then compare. See NSString Class Reference.

For NSURL equalvent, use:

NSURL *resolvedURL = [url URLByResolvingSymlinksInPath];

See NSURL Class Reference.

Cai
  • 3,609
  • 2
  • 19
  • 39
  • How about if they are of NSURL? – chubao Mar 10 '16 at 01:34
  • Then you use `URLByResolvingSymlinksInPath` of `NSURL`. Or convert `NSURL` to `NSString` path before the comparison. – Cai Mar 10 '16 at 01:37
  • I found what I need in this category file: https://github.com/DigitalLeaves/NSURLEquality/blob/master/NSURLTest/NSURL%2BRealEquality.m – chubao Mar 10 '16 at 02:12