【问题标题】:Proper parameters for closeAllDocumentsWithDelegatecloseAllDocumentsWithDelegate 的正确参数
【发布时间】:2016-06-27 23:35:48
【问题描述】:

我正在编写一个需要在一个窗口中包含多个文档的应用程序(有人问过here。所以我不能让它成为“基于文档的应用程序”,但我仍在尝试使用用于保存单个文件的文档架构。

我特别想做的是关闭所有打开的文档,如果它们已被修改,最好保存它们。似乎最好的方法是调用共享文档控制器的 closeAllDocumentsWithDelegate 方法。但我不知道要传递什么参数。文档说是:

func closeAllDocumentsWithDelegate(_delegate: AnyObject?, didCloseAllSelector didCloseAllSelector:选择器, contextInfo contextInfo: UnsafeMutablePointer)

不过,他们没有说这些是什么意思。我应该调用什么委托、选择器和 contextInfo?我找不到成功使用此方法的示例。我试着看看我是否可以摆脱 self 和 nil 只是为了让它编译:

SharedDocumentController.sharedDocumentController().closeAllDocumentsWithDelegate(self, didCloseAllSelector: nil, contextInfo: nil)

然后我收到错误“线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0)”,I'm told is a null pointer exception。因此,至少其中一个 nil 是不可行的,可能是因为在某处强制展开。很好,但我会放什么?

编辑:按照下面的要求,我附上了堆栈跟踪的图片:

控制台:

2016-06-29 12:00:46.397 FourthDraftBrouillon[54275:6093667] An uncaught exception was raised
2016-06-29 12:00:46.397 FourthDraftBrouillon[54275:6093667] *** -[__NSArray0 objectAtIndex:]: index 2 beyond bounds for empty NSArray
2016-06-29 12:00:46.398 FourthDraftBrouillon[54275:6093667] (
    0   CoreFoundation                      0x00007fff981034f2 __exceptionPreprocess + 178
    1   libobjc.A.dylib                     0x00007fff888b8f7e objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff98123205 -[__NSArray0 objectAtIndex:] + 101
    3   libswiftCore.dylib                  0x0000000100075f4d _TFVs12_ArrayBuffer19_getElementSlowPathfSiPs9AnyObject_ + 125
    4   libswiftCore.dylib                  0x0000000100075370 _TFVs12_ArrayBuffer10getElementfTSi20wasNativeTypeCheckedSb_x + 144
    5   libswiftCore.dylib                  0x000000010008e60a _TFSag9subscriptFSix + 138
    6   FourthDraftBrouillon                0x0000000100007c6f _TFC20FourthDraftBrouillon14ViewController14collectionViewfTCSo16NSCollectionView35itemForRepresentedObjectAtIndexPathCSo11NSIndexPath_CSo20NSCollectionViewItem + 799
    7   FourthDraftBrouillon                0x0000000100007e2f _TToFC20FourthDraftBrouillon14ViewController14collectionViewfTCSo16NSCollectionView35itemForRepresentedObjectAtIndexPathCSo11NSIndexPath_CSo20NSCollectionViewItem + 79
    8   AppKit                              0x00007fff92bc40f6 -[_NSCollectionViewDataSourceAdapter collectionView:itemForRepresentedObjectAtIndexPath:] + 487
    9   UIFoundation                        0x00007fff95acd3ff -[_NSCollectionViewCore _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 81
    10  UIFoundation                        0x00007fff95ace6f6 -[_NSCollectionViewCore _updateVisibleCellsNow:] + 2167
    11  UIFoundation                        0x00007fff95ad0b24 -[_NSCollectionViewCore _layoutItems] + 208
    12  AppKit                              0x00007fff92bb76aa -[NSCollectionView layout] + 80
    13  AppKit                              0x00007fff923f14eb -[NSView _doLayout] + 53
    14  AppKit                              0x00007fff923f11a3 -[NSView _layoutSubtreeWithOldSize:] + 324
    15  AppKit                              0x00007fff923f13ff -[NSView _layoutSubtreeWithOldSize:] + 928
    16  AppKit                              0x00007fff923f13ff -[NSView _layoutSubtreeWithOldSize:] + 928
    17  AppKit                              0x00007fff923f13ff -[NSView _layoutSubtreeWithOldSize:] + 928
    18  AppKit                              0x00007fff923f13ff -[NSView _layoutSubtreeWithOldSize:] + 928
    19  AppKit                              0x00007fff923f06dc -[NSView layoutSubtreeIfNeeded] + 950
    20  AppKit                              0x00007fff92410025 -[NSWindow(NSConstraintBasedLayout) _layoutViewTree] + 82
    21  AppKit                              0x00007fff92482603 -[NSWindow(NSConstraintBasedLayout) layoutIfNeeded] + 244
    22  AppKit                              0x00007fff92b191f5 ___NSWindowGetDisplayCycleObserver_block_invoke6358 + 218
    23  AppKit                              0x00007fff924945fc __37+[NSDisplayCycle currentDisplayCycle]_block_invoke + 719
    24  QuartzCore                          0x00007fff91702f71 _ZN2CA11Transaction19run_commit_handlersE18CATransactionPhase + 85
    25  QuartzCore                          0x00007fff9170242c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 160
    26  QuartzCore                          0x00007fff917020ec _ZN2CA11Transaction6commitEv + 508
    27  AppKit                              0x00007fff925d9650 -[_NSScrollingConcurrentMainThreadSynchronizer _synchronize:completionHandler:] + 391
    28  AppKit                              0x00007fff925d9496 __80-[_NSScrollingConcurrentMainThreadSynchronizer initWithSharedData:constantData:]_block_invoke + 144
    29  libdispatch.dylib                   0x00000001005cbcc5 _dispatch_client_callout + 8
    30  libdispatch.dylib                   0x00000001005deba6 _dispatch_source_latch_and_call + 3387
    31  libdispatch.dylib                   0x00000001005d0cd8 _dispatch_source_invoke + 978
    32  libdispatch.dylib                   0x00000001005e3012 _dispatch_main_queue_callback_4CF + 1153
    33  CoreFoundation                      0x00007fff980b89e9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    34  CoreFoundation                      0x00007fff980778dd __CFRunLoopRun + 1949
    35  CoreFoundation                      0x00007fff98076ed8 CFRunLoopRunSpecific + 296
    36  HIToolbox                           0x00007fff89e0b935 RunCurrentEventLoopInMode + 235
    37  HIToolbox                           0x00007fff89e0b76f ReceiveNextEventCommon + 432
    38  HIToolbox                           0x00007fff89e0b5af _BlockUntilNextEventMatchingListInModeWithFilter + 71
    39  AppKit                              0x00007fff9233cefa _DPSNextEvent + 1067
    40  AppKit                              0x00007fff9233c32a -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 454
    41  AppKit                              0x00007fff92330e84 -[NSApplication run] + 682
    42  AppKit                              0x00007fff922fa46c NSApplicationMain + 1176
    43  FourthDraftBrouillon                0x0000000100008d04 main + 84
    44  libdyld.dylib                       0x00007fff930135ad start + 1
    45  ???                                 0x0000000000000003 0x0 + 3
)
2016-06-29 12:00:46.399 FourthDraftBrouillon[54275:6093667] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 2 beyond bounds for empty NSArray'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff981034f2 __exceptionPreprocess + 178
    1   libobjc.A.dylib                     0x00007fff888b8f7e objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff98123205 -[__NSArray0 objectAtIndex:] + 101
    3   libswiftCore.dylib                  0x0000000100075f4d _TFVs12_ArrayBuffer19_getElementSlowPathfSiPs9AnyObject_ + 125
    4   libswiftCore.dylib                  0x0000000100075370 _TFVs12_ArrayBuffer10getElementfTSi20wasNativeTypeCheckedSb_x + 144
    5   libswiftCore.dylib                  0x000000010008e60a _TFSag9subscriptFSix + 138
    6   FourthDraftBrouillon                0x0000000100007c6f _TFC20FourthDraftBrouillon14ViewController14collectionViewfTCSo16NSCollectionView35itemForRepresentedObjectAtIndexPathCSo11NSIndexPath_CSo20NSCollectionViewItem + 799
    7   FourthDraftBrouillon                0x0000000100007e2f _TToFC20FourthDraftBrouillon14ViewController14collectionViewfTCSo16NSCollectionView35itemForRepresentedObjectAtIndexPathCSo11NSIndexPath_CSo20NSCollectionViewItem + 79
    8   AppKit                              0x00007fff92bc40f6 -[_NSCollectionViewDataSourceAdapter collectionView:itemForRepresentedObjectAtIndexPath:] + 487
    9   UIFoundation                        0x00007fff95acd3ff -[_NSCollectionViewCore _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 81
    10  UIFoundation                        0x00007fff95ace6f6 -[_NSCollectionViewCore _updateVisibleCellsNow:] + 2167
    11  UIFoundation                        0x00007fff95ad0b24 -[_NSCollectionViewCore _layoutItems] + 208
    12  AppKit                              0x00007fff92bb76aa -[NSCollectionView layout] + 80
    13  AppKit                              0x00007fff923f14eb -[NSView _doLayout] + 53
    14  AppKit                              0x00007fff923f11a3 -[NSView _layoutSubtreeWithOldSize:] + 324
    15  AppKit                              0x00007fff923f13ff -[NSView _layoutSubtreeWithOldSize:] + 928
    16  AppKit                              0x00007fff923f13ff -[NSView _layoutSubtreeWithOldSize:] + 928
    17  AppKit                              0x00007fff923f13ff -[NSView _layoutSubtreeWithOldSize:] + 928
    18  AppKit                              0x00007fff923f13ff -[NSView _layoutSubtreeWithOldSize:] + 928
    19  AppKit                              0x00007fff923f06dc -[NSView layoutSubtreeIfNeeded] + 950
    20  AppKit                              0x00007fff92410025 -[NSWindow(NSConstraintBasedLayout) _layoutViewTree] + 82
    21  AppKit                              0x00007fff92482603 -[NSWindow(NSConstraintBasedLayout) layoutIfNeeded] + 244
    22  AppKit                              0x00007fff92b191f5 ___NSWindowGetDisplayCycleObserver_block_invoke6358 + 218
    23  AppKit                              0x00007fff924945fc __37+[NSDisplayCycle currentDisplayCycle]_block_invoke + 719
    24  QuartzCore                          0x00007fff91702f71 _ZN2CA11Transaction19run_commit_handlersE18CATransactionPhase + 85
    25  QuartzCore                          0x00007fff9170242c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 160
    26  QuartzCore                          0x00007fff917020ec _ZN2CA11Transaction6commitEv + 508
    27  AppKit                              0x00007fff925d9650 -[_NSScrollingConcurrentMainThreadSynchronizer _synchronize:completionHandler:] + 391
    28  AppKit                              0x00007fff925d9496 __80-[_NSScrollingConcurrentMainThreadSynchronizer initWithSharedData:constantData:]_block_invoke + 144
    29  libdispatch.dylib                   0x00000001005cbcc5 _dispatch_client_callout + 8
    30  libdispatch.dylib                   0x00000001005deba6 _dispatch_source_latch_and_call + 3387
    31  libdispatch.dylib                   0x00000001005d0cd8 _dispatch_source_invoke + 978
    32  libdispatch.dylib                   0x00000001005e3012 _dispatch_main_queue_callback_4CF + 1153
    33  CoreFoundation                      0x00007fff980b89e9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    34  CoreFoundation                      0x00007fff980778dd __CFRunLoopRun + 1949
    35  CoreFoundation                      0x00007fff98076ed8 CFRunLoopRunSpecific + 296
    36  HIToolbox                           0x00007fff89e0b935 RunCurrentEventLoopInMode + 235
    37  HIToolbox                           0x00007fff89e0b76f ReceiveNextEventCommon + 432
    38  HIToolbox                           0x00007fff89e0b5af _BlockUntilNextEventMatchingListInModeWithFilter + 71
    39  AppKit                              0x00007fff9233cefa _DPSNextEvent + 1067
    40  AppKit                              0x00007fff9233c32a -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 454
    41  AppKit                              0x00007fff92330e84 -[NSApplication run] + 682
    42  AppKit                              0x00007fff922fa46c NSApplicationMain + 1176
    43  FourthDraftBrouillon                0x0000000100008d04 main + 84
    44  libdyld.dylib                       0x00007fff930135ad start + 1
    45  ???                                 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminating with uncaught exception of type NSException

【问题讨论】:

    标签: swift macos cocoa


    【解决方案1】:

    这个方法只是对所有打开的窗口执行-canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:。所以看看它的文档。 (以上链接。)

    简写形式:您可以传递一个委托对象,该对象将通过响应您传递的选择器的方法获知关闭请求。

    假设应该通知的对象是应用程序委托,然后您将其作为delegate 参数传递并在应用程序委托中编写一个方法,您将其选择器作为selector 参数传递。在 Swift 中,您必须使用 @objc 装饰该方法(Swift: function)。

    contextInfo 是要传递或不传递的任何用户可定义数据。它只是通过。它是从代码中的调用者到委托方法的“通信通道”,即。 e.表明关闭的原因。你可以给它传递nil

    即使我没有测试它,但通常你也可以通过nildelegate。在这种情况下,您应该能够为选择器传递NULL(它不是类型)。就试一试吧。那么你就不需要空方法了。

    【讨论】:

    • 选择器是传递给canCloseDocument还是在所有文档关闭后调用一次?
    • 如果所有文档都关闭,则调用 didCloseAllSelector 回调方法,否则为 NO。使用回调传递 contextInfo 对象。 didCloseAllSelector 回调方法应具有以下签名:
    • 啊,我明白了。编辑那个。
    • 谢谢...所以,在提出这个问题之前,我确实查看了 -canCloseDocumentWithDelegate 的文档,但它对我来说并不比 closeAllDocumentsWithDelegate 更清楚。我不知道 contextInfo 对象是什么或我会通过哪个对象,并且对选择器是什么只有一个模糊的理论概念。假设我只想关闭文档而不再发生任何事情 - 我可以在应用程序委托中编写一个空白方法供选择器调用,并将应用程序委托作为委托调用吗?然后我的 contextInfo 是什么 - 只要选择器不是,如果 that 为 nil 可以吗?
    • 尝试了上面 Amin 回复的编辑版本,将所有内容都归零。选择器的 NULL 将无法编译:Xcode 说“无法将 '()' 类型的值转换为预期的参数类型 'Selector'”。所以我尝试将选择器设置为 nil。现在,当我运行代码的相关部分时,应用程序崩溃并在控制台上喷出一堆东西,最值得注意的是: > 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArray0 objectAtIndex:]:空 NSArray' > libc++abi.dylib 的索引 3 超出范围:以 NSException 类型的未捕获异常终止
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2021-01-11
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2020-12-13
    • 2011-06-23
    相关资源
    最近更新 更多