我正在尝试将某个文件扩展名与我的macOS应用程序相关联,我已经按照本指南设法做到了这一点,并且可以将我的文件拖到我的应用程序中。
当我拖动我的文件时,我在控制台中得到这个异常:
2017-11-06 09:56:23.944202 0000测试应用程序[64192:2356795][常规]readFromData:类型:错误:是子类责任,但尚未被覆盖
201711-06 09:56:23.958166 0000测试应用[64192:2356795][常规](
0 CoreFoundation 0x00007fff45f560fb exception预处理171
1 libobjc.Adylib 0x00007fff6c844c76 objc_exception_othrow 48
2 CoreFoundation x0007fff45fe7bfd[NSE异常提升:格式:]205
3 AppKit 0x00007fff33a0b860-[NS文档读取数据:类型:错误:]253
4 AppKit 0x00007 fff43a0b544-[NSDocument readFromURL:类型:错误:]620
5 AppKit 0x00007fff436eb9b9-[NSDocument_initWithContentsOfURL:类型错误:]172
6 AppKit 0x0007fff436eb 89e-[NSDDocument initWithContentsOfURL:类型类型错误:]231
7 AppKit 0x00007 fff437c12dd-[NSDDocumentController makeDocumentWithContentsfURL:类型出错:]628
8 AppKit 0x00000fff443a42578 __97-[NSDocumentController makeDocumentWithContentsOfURL:alternateContents:类型:completionHandler:]_block_invoke 91
9 AppKit 0x00007fff43a42512-[NSDocument Controller makeDocumentWithContentsOfURL:AlternateContent:类型:completion Handler:]176
10 AppKit 0x00007 fff437c0493 __80-[NSDDocumentController openDocumentWithContents OfURL:display:completionHandler:]_block-invoke 880
11AppKit 0x00007fff43a411b9 __144-[NSDocumentController _coordinateReadingAndGetAlternateContentsForOpeningDocumentAtURL:解析符号链接:then ContinueOnThreadWithAccessor:]_block_invoke_431
12 AppKit 0x00007 fff43a 41542 __144-[NSDocumentController _coordiateReadingandGetAlternativeContentsForOpening DocumentAtURL:resolvingSymlinks:then Continue OnThreadWithAccess:]_block-invoke_2.970149
13 AppKit 0x00007fff43a4147d __144-[NSDocumentController _coordinateReadingAndGetAlternateContentsForOpeningDocumentAtURL:解析符号链接:then ContinueOnThreadWithAccessor:]_block_invoke.969 138
14 AppKit 0x00007 fff43a 4147f __144-[NSDocumentController _coordiateReadingandGetAlternativeContentsForOpening DocumentAtURL:resolvingSymlinks:then Continue OnThreadWithAccess:]_block_invoke.964 254
15核心基础0x00007fff45ee52c __CFRUNLOOP_IS_CALLING_OUT_TO_A_block 12
16核心基础0x00007 fff45ed0f43 __CFRunLoopDoBlocks 275
17核心基础0x0007fff45d0d08 __CFRunLoopRun 3128
18核心基础0x0007 fff45ecfe43 CFRUNLOOP运行特定483
19 HIToolbox 0x00007fff451ef866 RunCurrentEventLoopInMode 286
20 HIToolbox0x00007fff451ef5d6 ReceiveNextEventCommon 613
21 HIToolbox 0x00007fff451ef354 _BlockUntilNextEventMatchingListInModeWithFilter 64
22 AppKit 0x00007fff3434ec9f7 _DPSNextEvent 2085
23 AppKit 0x0007fff43c81d98-[NApplication(NSEvent)_nextEventMatchingEventMask:until Date:inMode:出列:]3044
24 AppKit 0x00007 fff4434e1805-[NApplication run]764
25 AppKit0x00007fff434b09a6 NSApplicationMain 804
26测试应用程序0x000000010004cd1d main 13
27 libdyld.dylib 0x00007fff6d433145启动1
28???0x0000000000000003 0x0 3
我尝试实现 NSApplicationDelegate openFile 函数,但是当我将文件拖到应用程序上时,它们都没有被调用:
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func application(_ application: NSApplication, open urls: [URL]) {
}
func application(_ sender: NSApplication, openFile filename: String) -> Bool {
return false
}
func application(_ sender: NSApplication, openFiles filenames: [String]) {
}
func application(_ sender: NSApplication, openTempFile filename: String) -> Bool {
return false
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
我如何访问“拖入”文件的文件名和内容?我用的是XCode 9.0.1和Swift 4。
编辑:
这是我的信息列表
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>tal</string>
</array>
<key>CFBundleTypeIconFile</key>
<string></string>
<key>CFBundleTypeMIMETypes</key>
<array>
<string></string>
</array>
<key>CFBundleTypeName</key>
<string>Layout File</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.testapp.layout</string>
</array>
<key>LSTypeIsPackage</key>
<integer>0</integer>
<key>NSDocumentClass</key>
<string>NSDocument</string>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>v1.0.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string></string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.video</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2017. All rights reserved.</string>
<key>NSMainStoryboardFile</key>
<string>Main</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeDescription</key>
<string>Layout File</string>
<key>UTTypeIconFile</key>
<string></string>
<key>UTTypeIdentifier</key>
<string>com.testapp.layout</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>tal</string>
</array>
</dict>
</dict>
<dict/>
</array>
</dict>
</plist>
所以我本来希望应用程序(_ sender:NSApplication,openFile 文件名:字符串)
在我把文件拖到应用程序上时被调用,当它位于 Dock 中时......
这里有许多要点需要解决:
> < li >如果您在macOS 10.12或更低版本中工作,您应该实现openFile或openFiles。openURLs是10.13 -但你肯定不需要两者都用 < li >将为多个文件中的每个文件调用openFile如果存在openFiles,即使您只打开一个文件,也不会调用openFile will > < li>
我不知道openTempFile(或openUntitledFile)是什么;我也没办法让任何人打电话
当您成功向系统注册UTI时,openFile(s)会被调用;在Finder中双击适当的文件,选择“打开”或将文件拖到应用程序的停靠图标上,所有这些都会发送一条打开消息,由应用程序代表处理。我不清楚“当我拖动我的文件时”是什么意思:如果您将文件拖到Finder中的应用程序图标上,则应该调用openFile(s);你的措辞让我认为你的拖动目标是应用程序界面的一部分,在这种情况下,你需要自己处理拖动。
还要检查-因为链接(iOS)指南没有解决这个问题-您是否设置了NSDocument类:您的示例显示“NSDocument”,但您的应用程序极有可能使用自定义子类;因此您需要将“类”设置为该名称。