gpt4 book ai didi

iphone - 如何在 iOS 中按修改日期对文件进行排序

转载 作者:可可西里 更新时间:2023-11-01 03:38:55 25 4
gpt4 key购买 nike

我使用 NSFileManager 检索文件夹中的文件,我想按修改日期对它们进行排序。怎么做?

谢谢。

最佳答案

到目前为止你尝试了什么?

我还没有这样做,但快速浏览一下文档让我认为您应该尝试以下操作:

  1. 调用 -contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: 并指定 NSURLContentModificationDateKey 作为键之一。
  2. 您将得到一个 NSURL 对象数组,然后您可以使用 NSArray 方法对其进行排序,例如 -sortedArrayUsingComparator:
  3. 传入比较器 block ,使用 -getResourceValue:forKey:error: 查找每个 NSURL 的修改日期。

更新:当我写上面的答案时,-getResourceValue:forKey:error: 存在于 iOS 中但没有做任何事情。该方法现在可用于 iOS 5。以下代码将记录应用程序的资源文件,后跟相应修改日期的列表:

NSFileManager *manager = [NSFileManager defaultManager];
NSArray *files = [manager contentsOfDirectoryAtURL:[[NSBundle mainBundle] resourceURL]
includingPropertiesForKeys:[NSArray arrayWithObject:NSURLContentModificationDateKey]
options:nil
error:nil];
NSMutableArray *dates = [NSMutableArray array];
for (NSURL *f in files) {
NSDate *d = nil;
if ([f getResourceValue:&d forKey:NSURLContentModificationDateKey error:nil]) {
[dates addObject:d];
}
}
NSLog(@"Files: %@", files);
NSLog(@"Dates: %@", dates);

关于iphone - 如何在 iOS 中按修改日期对文件进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6353168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com