chubao
chubao

Reputation: 6011

Compare two file paths in NSStrings including links

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?

Upvotes: 2

Views: 486

Answers (1)

Cai
Cai

Reputation: 3659

Try using:

NSString *resolvedPath = [path stringByResolvingSymlinksInPath];

then compare. See NSString Class Reference.

For NSURL equalvent, use:

NSURL *resolvedURL = [url URLByResolvingSymlinksInPath];

See NSURL Class Reference.

Upvotes: 3

Related Questions