Just My Life & My Work

先前已寫過[iOS] 列出資料夾裡的檔案名稱 (List Contents of Directory),然而現在想要以最後修改時間列出檔案名稱 (List Contents of Directory By Last Modification Date),直接來看程式碼:

/**
 Theme: List Contents of Directory By Last Modification Date
 IDE: Xcode 9
 Language: Objective C
 Date: 107/08/02
 Author: HappyMan
 Blog: https://cg2010studio.com/
 */

+(NSArray *)listFileAtPathSortedByModificationDate:(NSString *)folderPath
{
    NSError* error = nil;
    NSArray* filesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error];
    if(error != nil) {
        NSLog(@"Error in reading files: %@", [error localizedDescription]);
        return @[];
    }

    // sort by creation date
    NSMutableArray* filesAndProperties = [NSMutableArray arrayWithCapacity:[filesArray count]];
    for(NSString* file in filesArray) {
        NSString* filePath = [folderPath stringByAppendingPathComponent:file];
        NSDictionary* properties = [[NSFileManager defaultManager]
                                    attributesOfItemAtPath:filePath
                                    error:&error];
        NSDate* modDate = [properties objectForKey:NSFileModificationDate];

        if(error == nil)
        {
            [filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                           file, @"fileName",
                                           modDate, @"lastModificationDate",
                                           nil]];
        }
    }

    // sort using a block
    // order inverted as we want latest date first
    NSArray* sortedFiles = [filesAndProperties sortedArrayUsingComparator:
                            ^(id path1, id path2)
                            {
                                // compare
                                NSComparisonResult comp = [[path1 objectForKey:@"lastModificationDate"] compare:
                                                           [path2 objectForKey:@"lastModificationDate"]];
                                // invert ordering
                                if (comp == NSOrderedDescending) {
                                    comp = NSOrderedAscending;
                                }
                                else if(comp == NSOrderedAscending){
                                    comp = NSOrderedDescending;
                                }
                                return comp;
                            }];

    return sortedFiles;
}

以我的範例取得:

<__NSArrayI 0x137459190>(
{
fileName = “2018-08-02 10:17:20~2018-08-02 10:19:20.ecg";
lastModificationDate = “2018-08-02 02:19:24 +0000″;
},
{
fileName = “2018-08-02 10:11:32~2018-08-02 10:13:32.ecg";
lastModificationDate = “2018-08-02 02:14:48 +0000″;
},
{
fileName = “2018-08-02 10:09:28~2018-08-02 10:11:28.ecg";
lastModificationDate = “2018-08-02 02:11:31 +0000″;
},
{
fileName = “2018-08-02 10:06:47~2018-08-02 10:08:47.ecg";
lastModificationDate = “2018-08-02 02:08:50 +0000″;
},
{
fileName = “2018-08-02 09:02:28~2018-08-02 09:04:28.ecg";
lastModificationDate = “2018-08-02 01:04:30 +0000″;
},
{
fileName = “2018-07-31 23:40:03~2018-07-31 23:42:03.ecg";
lastModificationDate = “2018-07-31 15:42:07 +0000″;
},
{
fileName = “2018-07-31 14:43:38~2018-07-31 14:45:38.ecg";
lastModificationDate = “2018-07-31 06:45:40 +0000″;
},
{
fileName = “2018-07-31 14:40:58~2018-07-31 14:42:58.ecg";
lastModificationDate = “2018-07-31 06:43:01 +0000″;
}
)

參考:Get directory contents in date modified order

廣告

隨意留個言吧:)~

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

標籤雲

%d 位部落客按了讚: