【发布时间】:2011-10-17 20:14:33
【问题描述】:
我正在尝试设置文件的创建和修改日期。
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDate *now = [NSDate date];
NSDictionary *timestampAttr = [NSDictionary dictionaryWithObjectsAndKeys:
now, NSFileCreationDate, now, NSFileModificationDate, nil];
BOOL ret = [fileManager setAttributes:timestampAttr ofItemAtPath:path error:&err];
文件的修改日期已成功修改。创建日期不变。为什么?
ret 为真,err 为 nil。 -attributesOfItemAtPath 返回一个字典,其中包含 timestampAttr 中的两个键以及正确的(修改的)修改日期和不正确的(未修改的)创建日期。
编辑:使用 OS X 10.6 版。 Xcode 项目的基础 SDK 为 10.5。文件位于我计算机唯一的硬盘驱动器(无 RAID)上,位于我的主文件夹内的一个文件夹中。 如果有影响,该文件位于应用程序包中。
编辑:这个简单的例子有效:
#import <Cocoa/Cocoa.h>
int main(void)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSFileManager *m = [NSFileManager defaultManager];
NSString *path = ...;
[m setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSDate date],NSFileCreationDate,nil] ofItemAtPath:path error:nil];
[pool drain];
return 0;
}
我将上面的代码粘贴到我最初询问的应用程序中,为与简单示例相同的文件设置属性(在我的桌面上)。当代码在应用程序内时,它不起作用。
编辑:好的,这太疯狂了。上面的简单示例正在运行(我的同事和我都看到它在我的计算机上运行)但现在它不起作用。
【问题讨论】:
标签: objective-c cocoa macos nsfilemanager