[英]Is there a way to get folder's creation date in Cocoa?
I tried with NSFileManager's attributesOfItemAtPath. 我尝试使用NSFileManager的attributesOfItemAtPath。 It works well with files, but not with folders.
它适用于文件,但不适用于文件夹。 Although in Apple's documentation , they claimed it should work either on file or folder.
虽然在Apple的文档中 ,他们声称它应该在文件或文件夹上工作。 But all I get is a nil value if I call this on folders.
但是如果我在文件夹上调用它,我得到的只是零值。
Code I use: 我使用的代码:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDictionary *folderAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:somePath error:nil];
NSLog(@"Creation date: %@", [dateFormatter stringFromDate:[folderAttributes objectForKey:NSFileCreationDate]]);
The output is always null to me. 输出对我来说总是空的。 My "somePath" is an NSString, with a format like this:
我的“somePath”是一个NSString,格式如下:
file://localhost/Users/username/...
Thoughts? 思考? Thanks!
谢谢!
If it's a path, don't use a URL. 如果是路径,请不要使用URL。 Use a path:
使用路径:
/Users/username/dir
Also, get used to the error parameter, it's really helpful. 另外,习惯了错误参数,这真的很有帮助。
NSError *error = nil;
NSDictionary *attr = [fm attributesOfItemAtPath:path error:&error];
if (error)
NSLog(@"%@", error) /* at least */
It will help you. 它会对你有所帮助。 And if for some reason you don't want it, notice the parameter is a pointer to object, so you may want to use
NULL
instead of nil
. 如果由于某种原因您不想要它,请注意参数是指向对象的指针,因此您可能希望使用
NULL
而不是nil
。
somePath
应格式化为直接POSIX路径 - 将其设置为/ Users / username / ... - 应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.