ios - NSUndoManager 错误为 "unrecognized selector sent to instance"

标签 ios swift

致力于在 iOS9 应用程序中实现 NSUndoManager。当前的撤消代码如下所示:

func setActiveColorData(colorData: ColorData) {
    if colorData != activeColorData {
        print(self)
        undoManager?.registerUndoWithTarget(self, selector: "setActiveColorData:", object: activeColorData!)
        undoManager?.setActionName("Change color")
        print("Set undo action")

        activeColorData = colorData
    }
}

ColorData 只是一个简单的类,其中包含三个 float (色调、饱和度、亮度)和一些实用方法。

一切正常,包括弹出撤消提示的摇动手势。但是一旦您单击撤消,应用程序就会崩溃并出现以下错误:

2016-02-25 16:26:58.225 Color[89399:5503615] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Color.PickerViewController setActiveColorData:]: unrecognized selector sent to instance 0x7fa080776820'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000105b75e65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000108159deb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000105b7e48d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000105acb90a ___forwarding___ + 970
    4   CoreFoundation                      0x0000000105acb4b8 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00000001066b2feb -[_NSUndoStack popAndInvoke] + 261
    6   Foundation                          0x00000001066b2162 -[NSUndoManager undoNestedGroup] + 424
    7   UIKit                               0x0000000106c46813 -[UIApplication alertView:clickedButtonAtIndex:] + 151
    8   UIKit                               0x0000000106f63589 -[UIAlertView _prepareToDismissForTappedIndex:] + 136
    9   UIKit                               0x0000000106f62fc7 __35-[UIAlertView _prepareAlertActions]_block_invoke50 + 53
    10  UIKit                               0x0000000106f54572 -[UIAlertController _dismissAnimated:triggeringAction:triggeredByPopoverDimmingView:] + 133
    11  UIKit                               0x0000000107604eb6 -[_UIAlertControllerView _handleActionSelectionGestureRecognizer:] + 694
    12  UIKit                               0x000000010711ee73 _UIGestureRecognizerSendTargetActions + 153
    13  UIKit                               0x000000010711b4e5 _UIGestureRecognizerSendActions + 162
    14  UIKit                               0x00000001071194e2 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843
    15  UIKit                               0x00000001071219a0 ___UIGestureRecognizerUpdate_block_invoke904 + 79
    16  UIKit                               0x000000010712183e _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
    17  UIKit                               0x000000010710f101 _UIGestureRecognizerUpdate + 2634
    18  UIKit                               0x0000000106ca6f8a -[UIWindow _sendGesturesForEvent:] + 1137
    19  UIKit                               0x0000000106ca81c0 -[UIWindow sendEvent:] + 849
    20  UIKit                               0x0000000106c56b66 -[UIApplication sendEvent:] + 263
    21  UIKit                               0x0000000106c30d97 _UIApplicationHandleEventQueue + 6844
    22  CoreFoundation                      0x0000000105aa1a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    23  CoreFoundation                      0x0000000105a9795c __CFRunLoopDoSources0 + 556
    24  CoreFoundation                      0x0000000105a96e13 __CFRunLoopRun + 867
    25  CoreFoundation                      0x0000000105a96828 CFRunLoopRunSpecific + 488
    26  GraphicsServices                    0x000000010a416ad2 GSEventRunModal + 161
    27  UIKit                               0x0000000106c36610 UIApplicationMain + 171
    28  Color                               0x00000001052a2a7d main + 109
    29  libdyld.dylib                       0x0000000108c9992d start + 1
    30  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我已经检查了 PickerViewController 实例和内存位置是否匹配(来自 print(self) 行,在本例中是 0x7fa080776820)。知道为什么它不能调用这个选择器吗?或者是别的什么?谢谢!

最佳答案

当内存不足时,我在照片编辑应用程序中看到了同样类型的崩溃。当我收到内存警告时,我会清除撤消管理器,但就我而言,我相信发生此崩溃是因为内存从撤消管理器下被删除。

如果相关:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/UndoArchitecture/Articles/CleaningUndoStack.html

关于ios - NSUndoManager 错误为 "unrecognized selector sent to instance",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35633063/

相关文章:

ios - 不使用dispatch_once_t转换Swift 3代码

android - 在调试时按下 Dart DevTools 中的调试按钮之前,无法按下我的 Flutter 应用程序中的按钮

swift - 创建自定义按钮形状 Swift 4

ios - Swift:哪个相当于 sscanf()?

ios - 在 iOS 中访问最近使用的表情符号列表

ios - 跨平台加解密

iphone - 如何获取 cookie 并将它们用于其他请求,如 POST ( iOS )?

ios - AVFoundation - 如何将 AVCaptureMovieFileDataOutput 保存到文件?

ios - 向标签添加模糊 View ?

ios - 与 Xcode 兼容性的 Swift 框架问题