title: Sorting an array of NSURL objects by creation date
body: <p>My caches images to a directory (/Library/Caches/ImageCache/).  When the directory exceeds a certain size I would like to delete the oldest file in the directory.  To accomplish this task I use NSFileManager to retrieve the directory contents.  I then try to sort this array by date and delete the oldest object. </p>
  
  <p>My problem is that my program crashes when I try to sort the array by the key NSURLCreationDateKey.</p>
  
  <pre><code>NSFileManager *defaultFM = [NSFileManager defaultManager];
  NSArray *keys = [NSArray arrayWithObjects:NSURLNameKey, NSURLCreationDateKey, nil];
  NSURL *cacheDirectory = [self photoCacheDirectory];  // method that returns cache URL
  NSArray *cacheContents = [defaultFM contentsOfDirectoryAtURL:cacheDirectory
                                    includingPropertiesForKeys:keys
                                                       options:NSDirectoryEnumerationSkipsSubdirectoryDescendants
                                                         error:nil];
  
  NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:NSURLCreationDateKey ascending:YES];
  NSArray *sortedArray = [cacheContents sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
  </code></pre>
  
  <p>The program crashes on the last line. With error:</p>
  
  <p><em>*</em> Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key NSURLCreationDateKey.'</p>