gpt4 book ai didi

cocoa - NSDocument 和 writeToURL :ofType:error: 的问题

转载 作者:行者123 更新时间:2023-12-03 17:18:02 25 4
gpt4 key购买 nike

我正在开发一个基于文档的应用程序,并且我想使用文档包作为我的文件格式。为此,我需要重写的 NSDocument 方法似乎是
-writeToURL:ofType:error:.

有时它会起作用,但仅在某些条件下有效。例如,此代码有效:

- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
NSFileWrapper *wrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
[wrapper addRegularFileWithContents:[@"please work" dataUsingEncoding:NSUTF8StringEncoding] preferredFilename:@"foobar"];
[wrapper writeToURL:absoluteURL options:NSFileWrapperWritingAtomic originalContentsURL:nil error:outError];

NSDictionary *metadata = [NSDictionary dictionaryWithObject:@"0.1" forKey:@"Version"];
NSURL *mdURL = [NSURL fileURLWithPath:[[absoluteURL path] stringByAppendingPathComponent:@"SiteInfo.plist"]];
[metadata writeToURL:mdURL atomically:YES];

return YES;
}

但是,这段代码没有(与上面相同,但去掉了 NSFileWrapper 位):

- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
NSDictionary *metadata = [NSDictionary dictionaryWithObject:@"0.1" forKey:@"Version"];
NSURL *mdURL = [NSURL fileURLWithPath:[[absoluteURL path] stringByAppendingPathComponent:@"SiteInfo.plist"]];
[metadata writeToURL:mdURL atomically:YES];

return YES;
}

上面的代码将这个神秘的错误放入控制台(“Lithograph”是我的应用程序的名称,“.site”是包扩展名):

NSDocument could not delete the temporary item at file://localhost/private/var/folders/qX/qXL705byGmC9LN8FpiVjgk+++TI/TemporaryItems/(A%20Document%20Being%20Saved%20By%20Lithograph%207)/Untitled%20Site.site. Here's the error:
Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x10059d160 "“Untitled Site.site” couldn’t be removed."

我是否必须在原始 URL 中写入一些内容才能将其他文件添加到包中?

最佳答案

如果您要从文档创建文件包装器,则应使用 -fileWrapperOfType:error: 而不是 -writeToURL:ofType:error:

您将为 Info.plist 文件构造一个文件包装器,将其插入文件夹文件包装器,然后返回文件夹包装器:

- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName error:(NSError **)outError
{
NSFileWrapper *wrapper = [[[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil] autorelease];
[wrapper addRegularFileWithContents:[@"please work" dataUsingEncoding:NSUTF8StringEncoding] preferredFilename:@"foobar"];
NSDictionary *metadata = [NSDictionary dictionaryWithObject:@"0.1" forKey:@"Version"];
NSString* errorDescription = nil;
NSData* dictionaryData = [NSPropertyListSerialization dataFromPropertyList:metadata format:NSPropertyListBinaryFormat_v1_0 errorDescription:&errorDescription];
if(!dictionaryData)
{
if(!errorDescription)
errorDescription = @"Unknown error";
if(outError)
*outError = [NSError errorWithDomain:@"YourErrorDomain" code:69 userInfo:[NSDictionary dictionaryWithObject:errorDescription forKey:NSLocalizedDescriptionKey]];
return nil;
}
[wrapper addRegularFileWithContents:dictionaryData preferredFilename:@"Info.plist"];
return wrapper;
}

关于cocoa - NSDocument 和 writeToURL :ofType:error: 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1584119/

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