Is there an method to get the contents of a folder in a particular order? I'd like an array of file attribute dictionaries (or just file names) ordered by date modified.
Right now, I'm doing it this way:
get an array with the file names
get the attributes of each file
store the file's path and modified date in a dictionary with the date as a key
Next I have to output the dictionary in date order, but I wondered if there's an easier way? If not, is there a code snippet somewhere which will do this for me?
Thanks.
回答1:
nall's code above pointed me in the right direction, but I think there are some mistakes in the code as posted above. For instance:
Why is filesAndProperties allocated using NMutableDictonary rather than an NSMutableArray?
+(NSDate*) getModificationDateForFileAtPath:(NSString*)path {struct tm* date;// create a time structurestruct stat attrib;// create a file attribute structure stat([path UTF8String],&attrib);// get the attributes of afile.txt date = gmtime(&(attrib.st_mtime));// Get the last modified time and put it into the time structureNSDateComponents*comps =[[NSDateComponents alloc] init];[comps setSecond: date->tm_sec];[comps setMinute: date->tm_min];[comps setHour: date->tm_hour];[comps setDay: date->tm_mday];[comps setMonth: date->tm_mon +1];[comps setYear: date->tm_year +1900];NSCalendar*cal =[NSCalendar currentCalendar];NSDate*modificationDate =[[cal dateFromComponents:comps] addTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT]];[comps release];return modificationDate;}
回答5:
Code does not work in iPhone SDK, full of compilation error. Please find updated code `