Objective-C:获取文件创建/上次修改日期?

Ale*_*kiy 13 objective-c

如何在特定位置获取文件的文件创建或上次修改日期,例如/Users/MYUSER/Downloads/text.txt

Ale*_*kiy 42

Rosetta Code的示例已弃用部件.这是获取文件修改日期的正确代码

NSString *path = @"/Users/Raven/Downloads/1.png";
NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
NSDate *result = [fileAttribs objectForKey:NSFileCreationDate]; //or NSFileModificationDate
NSLog(@"%@",result);
Run Code Online (Sandbox Code Playgroud)


slh*_*hck 5

NSFileManager *fm = [NSFileManager defaultManager];

// Pre-OS X 10.5
NSLog(@"%@", [[fm fileAttributesAtPath:@"input.txt" traverseLink:YES] fileModificationDate]);
[fm changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate]
              atPath:@"input.txt"];

// OS X 10.5+
NSLog(@"%@", [[fm attributesOfItemAtPath:@"input.txt" error:NULL] fileModificationDate]);
[fm setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate]
 ofItemAtPath:@"input.txt" error:NULL];
Run Code Online (Sandbox Code Playgroud)

来源:罗塞塔代码