我的应用程序需要自定义文档支持,但我没有从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];
}];
}
日Thnx!
答案 0 :(得分:0)
在Mojave,Xcode 10.1中有同样的问题。
最终解决问题的方法是对文档类型中的图标使用.ICNS文件。只要我尝试使用PNG文件,这些文件就永远不会在finder中显示。