gpt4 book ai didi

objective-c - 设置 NSFileCreationDate 无效

转载 作者:太空狗 更新时间:2023-10-30 03:42:03 26 4
gpt4 key购买 nike

我正在尝试设置文件的创建和修改日期。

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 为 true,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;
}

我将上面的代码粘贴到我最初询问的应用程序中,为与简单示例(在我的桌面上)相同的文件设置属性。当代码在应用程序内部时,它不起作用。

编辑:好吧,这太疯狂了。上面的简单示例可以正常工作(我的同事和我都看到它在我的计算机上工作)但现在它不工作了。

最佳答案

在进一步尝试这个简单的示例之后,我注意到它只适用于我最近修改过的文件。

通过在设置创建日期之前设置修改日期,我能够让简单的示例以及我询问的原始代码一致地工作。

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];

关于objective-c - 设置 NSFileCreationDate 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6905503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com