Dostępne są dwa kroki: pobranie listy plików wraz z datami ich utworzenia i ich sortowanie.
W celu uczynienia go łatwo sortować je później utworzyć obiekt posiadać ścieżkę z dniem modyfikacja:
@interface PathWithModDate : NSObject
@property (strong) NSString *path;
@property (strong) NSDate *modDate;
@end
@implementation PathWithModDate
@end
Teraz, aby uzyskać listę plików i folderów (nie głęboki wyszukiwarki), użyj tego:
- (NSArray*)getFilesAtPathSortedByModificationDate:(NSString*)folderPath {
NSArray *allPaths = [NSFileManager.defaultManager contentsOfDirectoryAtPath:folderPath error:nil];
NSMutableArray *sortedPaths = [NSMutableArray new];
for (NSString *path in allPaths) {
NSString *fullPath = [folderPath stringByAppendingPathComponent:path];
NSDictionary *attr = [NSFileManager.defaultManager attributesOfItemAtPath:fullPath error:nil];
NSDate *modDate = [attr objectForKey:NSFileModificationDate];
PathWithModDate *pathWithDate = [[PathWithModDate alloc] init];
pathWithDate.path = fullPath;
pathWithDate.modDate = modDate;
[sortedPaths addObject:pathWithDate];
}
[sortedPaths sortUsingComparator:^(PathWithModDate *path1, PathWithModDate *path2) {
// Descending (most recently modified first)
return [path2.modDate compare:path1.modDate];
}];
return sortedPaths;
}
Zauważ, że gdy tworzę tablicę obiektów PathWithDate używam sortUsingComparator
, aby umieścić je w odpowiedniej kolejności (wybrałem malejąco). Zamiast tego użyj daty utworzenia, zamiast tego użyj [attr objectForKey:NSFileCreationDate]
.
http://stackoverflow.com/questions/1523792/get-directory-contents-in-date-modified-order is to? Myślę, że możesz użyć NSFileCreationDate zamiast NSFileModificationDate –