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资源值.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...