Nat! bio photo

Nat!

Senior Mull

Twitter Github Twitch

-[NSURL standardizedURL] is maybe too eager to please

Standardizing NSURLs is useful, so that they can be compared with -isEqual:. A URL shouldn't be different, if the resource exists or not. But this is not the case with NSURL, which will return either file:///tmp/vfl-bochum-1848 or file:///tmp/vfl-bochum-1848/ depending on the existence of a directory at that path.

According to the documentation it should just do syntactical removal:

A copy of the URL with any instances of ".." or "." removed from its path. (read-only)

Here's the obligatory test code.

//
//  main.m
//  TestStandardizedURL
//
//  Created by Nat! on 14/12/14.
//  Copyright (c) 2014 Mulle kybernetiK. All rights reserved.
//

#import <Foundation/Foundation.h>


static void   remove_dir( NSFileManager *manager, NSString *path, BOOL okIfMissing)
{
   if( ! [manager removeItemAtPath:path
                             error:nil])
      if( ! okIfMissing)
         abort();
}


static void   create_dir( NSFileManager *manager, NSString *path)
{
   if( ! [manager createDirectoryAtPath:path
            withIntermediateDirectories:NO
                             attributes:nil
                                  error:nil])
      abort();
}


int   main( int argc, const char * argv[])
{
   NSFileManager   *manager;
   NSString        *path;
   NSURL           *url;
   
   @autoreleasepool
   {
      manager = [NSFileManager defaultManager];
      path    = @"/tmp/vfl-bochum-1848";

      remove_dir( manager, path, YES);

      url = [NSURL fileURLWithPath:path];
      url = [url standardizedURL];
      
      printf( "%s\n", [[url description] cString]);
      
      create_dir( manager, path);
      
      url = [NSURL fileURLWithPath:path];
      url = [url standardizedURL];
      
      printf( "%s\n", [[url description] cString]);
   }
    return 0;
}

Post a comment

All comments are held for moderation; basic HTML formatting accepted.

Name:
E-mail: (not published)
Website: