我的应用程序需要自定义文档支持,但我开始时没有在xcode中使用Document-template。通过遵循本教程等内容,我成功加载和保存了数据:https://www.brandpending.com/2016/02/21/opening-and-saving-custom-document-types-from-a-swift-cocoa-application/。但是一个问题是,我的自定义图标没有显示在我的文件上!它仍然是一个空白页图标 :(。
谢谢!
我做错了什么?
这是我的设置:
这是我的HSDocument代码:
-(BOOL)readFromURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError
{
NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithFile:[url path]];
self.archive = myDictionary;
return YES;
}
-(BOOL)writeToURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError
{
[NSKeyedArchiver archiveRootObject:self.archive toFile:[url path]];
return YES;
}
以下是我如何将数据载入/保存到文件中的方式:(我不想通过常规菜单项来触发它)
- (void)onLoadFile
{
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setCanChooseFiles:YES];
[panel setCanChooseDirectories:YES];
[panel makeKeyAndOrderFront:nil];
[panel setLevel:NSStatusWindowLevel];
[panel setCanCreateDirectories:NO];
[[self.preferencesWindow window] setLevel:kCGNormalWindowLevel];
[panel setFrameOrigin:CGPointZero];
[panel beginWithCompletionHandler:^(NSInteger result)
{
if (result == NSFileHandlingPanelOKButton)
{
for (NSURL *url in [panel URLs])
{
NSError *error;
HSDocument *document = [HSDocument new];
[document readFromURL:url ofType:@"my-identifier" error:&error];
[self setPresetsFromCollection:document.archive onStartup:NO];
}
}
[[self.preferencesWindow window] setLevel:NSStatusWindowLevel];
}];
}
- (void)onSaveFile
{
NSSavePanel *panel = [NSSavePanel savePanel];
[panel setMessage:@"Please select a location to save the file."];
[panel setAllowsOtherFileTypes:YES];
[panel setExtensionHidden:YES];
[panel setTitle:@"Saving file..."]; // Window title
[panel setCanCreateDirectories:YES];
[[self.preferencesWindow window] setLevel:kCGNormalWindowLevel];
[panel beginWithCompletionHandler:^(NSInteger result)
{
if (result == NSFileHandlingPanelOKButton)
{
NSURL* theUrl = [panel URL];
theUrl = [theUrl URLByAppendingPathExtension:@"hej"];
HSDocument *document = [HSDocument new];
document.archive = [self collectPresetsToSave];
NSError *error;
[document writeToURL:theUrl ofType:@"my-identifier" error:&error];
}
[[self.preferencesWindow window] setLevel:NSStatusWindowLevel];
}];
}
谢谢!