2011-06-15 97 views

回答

8

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

我没有这样做,而是快速浏览的文档让我觉得,你应该尝试以下方法:

  1. 呼叫-contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:并指定NSURLContentModificationDateKey的关键之一。
  2. 您将得到一个NSURL对象数组,然后您可以使用类似-sortedArrayUsingComparator:的NSArray方法进行排序。
  3. 传入一个比较器块,使用-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); 
+0

奇怪的是,对于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

+0

基金会框架对MacOS X和iOS来说很常见,所以它可能只是因为它很有用在MacOS X中。另一方面,它可能仍然值得尝试,希望文档不正确 - 偶尔会发生。 – Caleb 2011-06-23 07:06:19

+0

@Dustin,请注意,文档现在表明'getResourceValue:forKey:error:'在iOS 5中可用。我做了一个快速测试并确认它可以正常工作。我也会更新上面的答案。 – Caleb 2011-12-09 16:36:42

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); 
相关问题