gpt4 book ai didi

swift - closeAllDocumentsWithDelegate 的正确参数

转载 作者:行者123 更新时间:2023-11-30 13:17:41 26 4
gpt4 key购买 nike

我正在编写一个应用程序,需要在一个窗口中包含多个文档(正如询问 here 一样。所以我不能仅仅将其设为“基于文档的应用程序”,但我仍在尝试使用文档架构来保存各个文件。

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

func closeAllDocumentsWithDelegate(_ delegate: AnyObject?, didCloseAllSelector didCloseAllSelector: Selector, contextInfo contextInfo: UnsafeMutablePointer)

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

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

但随后我收到错误“Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)”,即I'm told is a null pointer exception 。因此,至少其中一个 nil 是不行的,可能是因为在某处强制展开。好吧,但是我会放什么呢?

编辑:根据下面的要求,我附上了堆栈跟踪的图片: stacktrace

控制台:

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

最佳答案

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

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

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

contextInfo 是任何用户可定义的要传递或不传递的数据。它只是简单地通过了。它是从代码中的调用者到委托(delegate)方法的“通信 channel ”,即。 e.表明关闭的原因。您可以为其传递 nil

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

关于swift - closeAllDocumentsWithDelegate 的正确参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38064830/

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