Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7009843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:54:51+00:00 2026-05-27T21:54:51+00:00

My caches images to a directory (/Library/Caches/ImageCache/). When the directory exceeds a certain size

  • 0

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.

My problem is that my program crashes when I try to sort the array by the key NSURLCreationDateKey.

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]];

The program crashes on the last line. With error:

* Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key NSURLCreationDateKey.’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:54:52+00:00Added an answer on May 27, 2026 at 9:54 pm

    EDIT : Better answer

    If that doesn’t work, you will have to write your own comparator block and get the dates to compare manually 🙁

    [cacheContents sortUsingComparator:^ (NSURL *a, NSURL *b) {
        // get the two dates
        id da = [[a resourceValuesForKeys:[NSArray arrayWithObject:NSURLCreationDateKey] error:nil] objectForKey:NSURLCreationDateKey];
        id db = [[b resourceValuesForKeys:[NSArray arrayWithObject:NSURLCreationDateKey] error:nil] objectForKey:NSURLCreationDateKey];
    
        // compare them
        return [da compare:db];
    }];
    

    (Same disclaimer still applies but I’m not even sure that will compile 😉 – you get the idea though!)


    Here is my first answer (included here for posterity but mostly it just shows how important it is to read the question properly 🙂

    It’s because you’re getting an array of NSURL objects; these don’t have a NSURLCreationDateKey property.

    Try this (disclaimer – not 100% it will work)

    NSString *key = [NSString stringWithFormat:@"fileAttributes.%@", NSURLCreationDateKey];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:key ascending:YES];
    

    Your key to sort by is a property of the fileAttributes dictionary which is, in turn, a property of your enumerator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently my application caches models in memcache like this: memcache.set(somekey, aModel) But Nicks' post
We have a multisite CMS that handles images and other files like this.. How
I'm building an app which caches images in the Documents directory of the app
I'm planning to create a module for my directory which would fetch images from
I keep getting this error 07-28 19:32:40.536: ERROR/error(534): java.io.IOException: No such file or directory
If we use an aspx page with a Caching Profile, the server caches images
Rails caches (file cache) per default domain-based, but is there a way to disable
I have custom table view cell with images (loaded from app document directory), labels,
I'm making a program that will display a few images from a directory beside
I'm storing my images in subdirectories under the cache directory. I have a cache

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.