5
A
回答
8
你到目前为止尝试过什么?
我没有这样做,而是快速浏览的文档让我觉得,你应该尝试以下方法:
- 呼叫
-contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:
并指定NSURLContentModificationDateKey
的关键之一。 - 您将得到一个NSURL对象数组,然后您可以使用类似
-sortedArrayUsingComparator:
的NSArray方法进行排序。 - 传入一个比较器块,使用
-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);
2
static static NSInteger contentsOfDirSort(NSString *left, NSString *right, void *ptr) {
(void)ptr;
struct stat finfo_l, r_finfo_r;
if(-1 == stat([left UTF8String], &finfo_l))
return NSOrderedSame;
if(-1 == stat([right UTF8String], &finfo_r))
return NSOrderedSame;
if(finfo_l.st_mtime < finfo_r.st_mtime)
return NSOrderedAscending;
if(finfo_l.st_mtime > finfo_r.st_mtime)
return NSOrderedDescending;
return NSOrderedSame;
}
现在,后来在你的代码。
NSMutableArray *mary = [NSMutableArray arrayWithArray:filePathsArray];
[mary sortUsingFunction:contentsOfDirSort context:nil];
// Use mary...
0
在分类过NSFileManager
:
static NSInteger contentsOfDirSort(NSURL *leftURL, NSURL *rightURL, void *ptr)
{
(void)ptr;
NSDate * dateLeft ;
[leftURL getResourceValue:&dateLeft
forKey:NSURLContentModificationDateKey
error:nil] ;
NSDate * dateRight ;
[leftURL getResourceValue:&dateRight
forKey:NSURLContentModificationDateKey
error:nil] ;
return [dateLeft compare:dateRight];
}
- (NSArray *)contentsOrderedByDateOfDirectoryAtPath:(NSURL *)URLOfFolder ;
{
NSArray *files = [self contentsOfDirectoryAtURL:URLOfFolder
includingPropertiesForKeys:[NSArray arrayWithObject:NSURLContentModificationDateKey]
options:0
error:nil];
return [files sortedArrayUsingFunction:contentsOfDirSort
context:nil] ;
}
0
快速方法:
-(void)dateModified
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
//--- Listing file by name sort
NSLog(@"\n File list %@",fileList);
int num;
//-- Listing file name with modified dated
for (NSString *s in fileList)
{
NSString *filestring = [documentsDirectory stringByAppendingFormat:@"/%@",s];
NSDictionary *filePathsArray1 = [[NSFileManager defaultManager] attributesOfItemAtPath:filestring error:nil];
NSString *modifiedDate = [filePathsArray1 objectForKey:NSFileModificationDate];
NSLog(@"\n Modified Day : %@", modifiedDate);
num=num+1;
}
}
2
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSMutableArray *originalImage = [[NSMutableArray alloc]init];
for (int i = 1; i < [imageFilenames count]; i++)
{
NSString *imageName = [NSString stringWithFormat:@"%@/%@",documentsDirectory,[imageFilenames objectAtIndex:i] ];
}
//---------sorting image by date modified
NSArray* filelist_date_sorted;
filelist_date_sorted = [imageFilenames sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSDictionary* first_properties = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", documentsDirectory, obj1] error:nil];
NSDate *first = [first_properties objectForKey:NSFileCreationDate];
NSDictionary *second_properties = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", documentsDirectory, obj2] error:nil];
NSDate *second = [second_properties objectForKey:NSFileCreationDate];
return [second compare:first];
}];
NSLog(@" Date sorted result is %@",filelist_date_sorted);
相关问题
- 1. 如何按修改日期通过php对文件进行排序
- 2. 如何根据修改的日期对图像进行排序?
- 3. 如何通过“修改日期”对此列表进行排序?
- 4. Android:如何按日期对FTPFile []文件列表进行排序?
- 5. 在asp.net中按日期对directoryinfo中的文件进行排序
- 6. 在vb.net中按日期对directoryinfo中的文件进行排序
- 7. 按日期对表进行排序SQlite
- 8. 按创建日期排序文件 - iOS
- 9. 对文件中的日期进行的内容修改
- 10. scandir()按修改日期排序
- 11. 如何在VB6中按日期对Excel数据进行排序
- 12. 如何使用Perl按升序对日期进行排序?
- 13. 如何根据创建日期对文件进行排序?
- 14. 在Mathematica中按文本日期列对数组进行排序
- 15. PHP按文件夹排序修改日期
- 16. 如何按日期排序mbox文件?
- 17. 如何按日期排序文件
- 18. 如何按日期和时间对帖子进行排序?
- 19. 如何按日期对数组进行排序?
- 20. 如何按日期添加对列进行排序?
- 21. 如何按日期对数据框进行排序
- 22. 如何按日期对DataGridView列(绑定到BindingSource)进行排序?
- 23. 如何按日期对$ _POST数组进行排序字符串
- 24. 如何按最后一次修改日期的时间顺序对“grep -l”的输出进行排序?
- 25. 按日历顺序对日期列进行排序R
- 26. 我试图根据修改日期对文件夹中的所有文件列表进行排序
- 27. 如何对数组中的日期对象进行排序?
- 28. Mercurial如何对文件进行修改?
- 29. 按日期分类器对邮件进行排序
- 30. 排序在PHP中通过创建/修改日期的文件
奇怪的是,对于getResourceValue文档:forKey:错误:说:“这种方法在未实现iOS,所以它不执行任何操作。“ 就在它下面,他们说“可用于iOS 4.0及更高版本”。 为什么他们会添加一个未实现的方法令人困惑。 = | 更多信息:http://www.themusingsofalostprogrammer.com/2011/06/what-were-they-thinking-apple.html – Dustin 2011-06-23 06:28:39
基金会框架对MacOS X和iOS来说很常见,所以它可能只是因为它很有用在MacOS X中。另一方面,它可能仍然值得尝试,希望文档不正确 - 偶尔会发生。 – Caleb 2011-06-23 07:06:19
@Dustin,请注意,文档现在表明'getResourceValue:forKey:error:'在iOS 5中可用。我做了一个快速测试并确认它可以正常工作。我也会更新上面的答案。 – Caleb 2011-12-09 16:36:42