从新到旧 (按创建日期
NSString* folder = [ZSML folder:FdrLocalMusic];
NSArray *paths = [[NSFileManager defaultManager] subpathsAtPath:folder];//取得文件列表
NSArray *sortedPaths = [paths sortedArrayUsingComparator:^(NSString * firstPath, NSString* secondPath) {//
NSString *firstUrl = [folder stringByAppendingPathComponent:firstPath];//获取前一个文件完整路径
NSString *secondUrl = [folder stringByAppendingPathComponent:secondPath];//获取后一个文件完整路径
NSDictionary *firstFileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath:firstUrl error:nil];//获取前一个文件信息
NSDictionary *secondFileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath:secondUrl error:nil];//获取后一个文件信息
id firstData = [firstFileInfo objectForKey:NSFileCreationDate];//获取前一个文件修改时间
id secondData = [secondFileInfo objectForKey:NSFileCreationDate];//获取后一个文件修改时间
return -[firstData compare:secondData];//升序
}];
for (NSString *path in sortedPaths) {
NSString* localPath = [NSString stringWithFormat:@"%@/%@", folder, path];
[poolMusicData addObject:@{@"path":localPath,@"name":path.stringByDeletingPathExtension}];
}
参考 https://blog.csdn.net/worthyzhang/article/details/45891675
不排序法
NSString* folder = [ZSML folder:FdrLocalMusic];
NSDirectoryEnumerator *myDirectoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:folder];
NSLog(@"%@", myDirectoryEnumerator);
for (NSString *path in myDirectoryEnumerator.allObjects) {
NSString* localPath = [NSString stringWithFormat:@"%@/%@", folder, path];
[poolMusicData addObject:@{@"path":localPath,@"name":path.stringByDeletingPathExtension}];
}