Thursday, May 9, 2013

NSDictionary NSFileModificationDate example ios


fileModificationDate - NSFileModificationDate

Returns the value for the key NSFileModificationDate.
- (NSDate *)fileModificationDate
Return Value
The value for the key NSFileModificationDate, or nil if the dictionary doesn’t have an entry for the key.
Discussion
This and the other file... methods are for use with a dictionary, such as those returned from the methodsfileAttributesAtPath:traverseLink: (NSFileManager), directoryAttributes(NSDirectoryEnumerator), and fileAttributes (NSDirectoryEnumerator), that represents the POSIX attributes of a file or directory. This method returns the date that the file’s data was last modified.
Example of [NSDictionary NSFileModificationDate]
while (file = [dirEnum nextObject]) {
    NSLog(@"File = %@",file);
    NSError *attributesError = nil;
    file = [docsDir stringByAppendingPathComponent:file];
    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:file error:&attributesError];

    NSDate *fileModificationDate = [fileAttributes objectForKey:NSFileModificationDate];
    NSLog(@"ModDate = %@",fileModificationDate);

    } 
}
Example of [NSDictionary NSFileModificationDate]
NSError * err;
NSFileManager * myManager = [NSFileManager defaultManager];
NSString * myPath = [[NSBundle mainBundle] bundlePath];
NSDictionary * myDict = [myManager attributesOfItemAtPath:myPath error:&err];
NSDate * myDate = [myDict objectForKey:@"NSFileModificationDate"];
NSLog(@"%@", myDate);


2011-01-26 21:32:29.985 scraptime2[11488:207] 2011-01-27 03:32:27 +0000
Example of [NSDictionary NSFileModificationDate]


    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsPath = [searchPaths objectAtIndex: 0]; 

    NSError* error = nil;
    NSArray* filesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath 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 = [iMgr.documentsPath stringByAppendingPathComponent:file];
        NSDictionary* properties = [[NSFileManager defaultManager]
                                    attributesOfItemAtPath:filePath
                                    error:&error];
        NSDate* modDate = [properties objectForKey:NSFileModificationDate];

        if(error == nil)
        {
            [filesAndProperties addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                           file, @"path",
                                           modDate, @"lastModDate",
                                           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:@"lastModDate"] compare:
                                                           [path2 objectForKey:@"lastModDate"]];
                                // invert ordering
                                if (comp == NSOrderedDescending) {
                                    comp = NSOrderedAscending;
                                }
                                else if(comp == NSOrderedAscending){
                                    comp = NSOrderedDescending;
                                }
                                return comp;                                
                            }];