微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – 在Cocoa中查找文件的上次访问日期

是否可以使用 cocoa在mac中获取文件/文件夹最后访问日期?
struct stat output;
    //int ret = stat([[[openPanel filenames] lastObject] UTF8String],&output);
    int ret = stat([[[openPanel filenames] lastObject] fileSystemRepresentation],&output);
    // error handling omitted for this example
    struct timespec accesstime = output.st_atimespec;

    NSDate *aDate = [NSDate dateWithTimeIntervalSince1970:accesstime.tv_sec];

    NSLog(@"Access Time %d,%@",ret,aDate);

根据上面的代码,我尝试了UTF8String和fileSystemRepresentation,但两者都给了我当前的日期和时间.如果我做错了,请告诉我.

解决方法

使用stat系统调用的C方式将在Objective-C中工作.

例如

struct stat output;
int ret = stat(aFilePath,&output);
// error handling omitted for this example
struct timespec accesstime = output.st_atime;

您应该通过将-fileSystemRepresentation发送到包含该路径的Nsstring来获取aFilePath.

另一种获得所需方法方法是在指向所需文件文件URL之前构造NSURL,并使用-resourceValuesForKeys:error:获取NSURLContentAccessDate资源值.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐