Re: dealing with alias files, finding URL to target file
Re: dealing with alias files, finding URL to target file
- Subject: Re: dealing with alias files, finding URL to target file
- From: Quincey Morris <email@hidden>
- Date: Fri, 15 Apr 2011 12:01:14 -0700
On Apr 15, 2011, at 11:10, Sean McBride wrote:
> _this_ is a PITA:
>
> - (NSURL*)URLByResolvingSymlinksAndAliases
> {
> NSURL* resultURL = [self URLByResolvingSymlinksInPath];
>
> NSError* error = nil;
> NSNumber* isAliasFile = nil;
> BOOL success = [resultURL getResourceValue:&isAliasFile
> forKey:NSURLIsAliasFileKey
> error:&error];
> if (success && [isAliasFile boolValue])
> {
> NSData* bookmarkData = [NSURL bookmarkDataWithContentsOfURL:resultURL
> error:&error];
> if (bookmarkData)
> {
> BOOL isStale = NO;
> NSURLBookmarkResolutionOptions options =
> (NSURLBookmarkResolutionWithoutUI |
> NSURLBookmarkResolutionWithoutMounting);
>
> NSURL* resolvedURL = [NSURL URLByResolvingBookmarkData:bookmarkData
> options:options
> relativeToURL:nil
> bookmarkDataIsStale:&isStale
> error:&error];
> if (resolvedURL)
> {
> resultURL = resolvedURL;
> }
> }
> }
>
> return resultURL;
> }
I'll ask the dumb question: what happens if you invoke bookmarkDataWithContentsOfURL on a non-alias file? If it correctly detects an error, your PITA code could be reduced to:
> - (NSURL*)URLByResolvingSymlinksAndAliases
> {
> NSURL* resultURL = [self URLByResolvingSymlinksInPath];
>
> NSData* bookmarkData = [NSURL bookmarkDataWithContentsOfURL:resultURL error: NULL];
> if (bookmarkData)
> {
> NSURL* resolvedURL = [NSURL URLByResolvingBookmarkData:bookmarkData
> options:(NSURLBookmarkResolutionWithoutUI | NSURLBookmarkResolutionWithoutMounting)
> relativeToURL:nil
> bookmarkDataIsStale:NULL
> error:NULL];
> if (resolvedURL)
> resultURL = resolvedURL;
>
> }
>
> return resultURL;
> }
which is a *lot* less unpleasant.
_______________________________________________
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