Thursday, May 9, 2013

NSDictionary fileCreationDate example ios


fileCreationDate

Returns the value for the NSFileCreationDate key.
- (NSDate *)fileCreationDate
Return Value
The value for the NSFileCreationDate key, or nil if the dictionary doesn’t have an entry for the key.
Example of [NSDictionary fileCreationDate]
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
for(int i=0;i<[dirContents count];i++)
{
NSDictionary* attrs = [fm attributesOfItemAtPath:[dirContents objectAtIndex:i] error:nil];

    if (attrs != nil) {
        NSDate *date = (NSDate*)[attrs fileCreationDate];
        NSLog(@"Date Created: %@", [date description]);

    }
    else {
        NSLog(@"Not found");
    }
}
Example of [NSDictionary fileCreationDate]
- (NSDictionary *) attributesForFile:(NSURL *)anURI {

// note: singleton is not thread-safe
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *aPath = [anURI path];

if (![fileManager fileExistsAtPath:aPath]) return nil;

NSError *attributesRetrievalError = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:aPath
                           error:&attributesRetrievalError];

if (!attributes) {
   NSLog(@"Error for file at %@: %@", aPath, attributesRetrievalError);
   return nil;
}

NSMutableDictionary *returnedDictionary = 
   [NSMutableDictionary dictionaryWithObjectsAndKeys:
        [attributes fileType], @"fileType",
        [attributes fileModificationDate], @"fileModificationDate",
        [attributes fileCreationDate], @"fileCreationDate",
        [NSNumber numberWithUnsignedLongLong:[attributes fileSize]], @"fileSize",
    nil];

return returnedDictionary;
}