jls*_*ker 6 macos cocoa objective-c nsfilemanager
我正在尝试在文件上设置创建和修改日期.
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];
Run Code Online (Sandbox Code Playgroud)
已成功修改文件的修改日期.创建日期不变.为什么?
ret
是真的,err
没有.-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;
}
Run Code Online (Sandbox Code Playgroud)
我将上面的代码粘贴到我最初询问的应用程序中,设置与简单示例相同的文件的属性(在我的桌面上).当代码在应用程序内部时,它不起作用.
编辑:好的,这很疯狂.上面的简单示例正在工作(我的同事和我都看到它在我的计算机上工作)但现在它无法正常工作.
在再次使用这个简单的例子之后,我注意到它只适用于我最近修改过的文件.
我能够通过在设置创建日期之前设置修改日期来获得简单示例以及我询问的原始代码.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDate *now = [NSDate date];
NSDictionary *creationDateAttr = [NSDictionary dictionaryWithObjectsAndKeys: now, NSFileCreationDate, nil];
NSDictionary *modificationDateAttr = [NSDictionary dictionaryWithObjectsAndKeys: now, NSFileModificationDate, nil];
[fileManager setAttributes:modificationDateAttr ofItemAtPath:path error:&err];
[fileManager setAttributes:creationDateAttr ofItemAtPath:path error:&err];
Run Code Online (Sandbox Code Playgroud)