SIGABRT/Unrecognized 选择器

SIGABRT/Unrecognized Selector

我的应用本质上是一组卡片,每张卡片上都有各种 messages/information。滑动以浏览这些卡片,通常是向右滑动。我目前收到此 sig abrt 错误:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

它也打印,这个:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[filterPageViewController askForPushNotifications]: unrecognized selector sent to instance 0x7f8b6d0acd80'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010fa92d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010f506deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010fa9bd3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010f9e1cfa ___forwarding___ + 970
    4   CoreFoundation                      0x000000010f9e18a8 _CF_forwarding_prep_0 + 120
    5   Lettuce                             0x000000010a2780a6 -[DraggableViewBackground cardSwipedRight:] + 2470
    6   Lettuce                             0x000000010a2d0ad5 -[DraggableView rightAction] + 453
    7   Lettuce                             0x000000010a2d02ed -[DraggableView afterSwipeAction] + 77
    8   Lettuce                             0x000000010a2cfe22 -[DraggableView beingDragged:] + 1922
    9   UIKit                               0x000000010d9c7b28 _UIGestureRecognizerSendTargetActions + 153
    10  UIKit                               0x000000010d9c419a _UIGestureRecognizerSendActions + 162
    11  UIKit                               0x000000010d9c2197 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843
    12  UIKit                               0x000000010d9ca655 ___UIGestureRecognizerUpdate_block_invoke898 + 79
    13  UIKit                               0x000000010d9ca4f3 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
    14  UIKit                               0x000000010d9b7e75 _UIGestureRecognizerUpdate + 2634
    15  UIKit                               0x000000010d54448e -[UIWindow _sendGesturesForEvent:] + 1137
    16  UIKit                               0x000000010d5456c4 -[UIWindow sendEvent:] + 849
    17  UIKit                               0x000000010d4f0dc6 -[UIApplication sendEvent:] + 263
    18  UIKit                               0x000000010d4ca553 _UIApplicationHandleEventQueue + 6660
    19  CoreFoundation                      0x000000010f9b8301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    20  CoreFoundation                      0x000000010f9ae22c __CFRunLoopDoSources0 + 556
    21  CoreFoundation                      0x000000010f9ad6e3 __CFRunLoopRun + 867
    22  CoreFoundation                      0x000000010f9ad0f8 CFRunLoopRunSpecific + 488
    23  GraphicsServices                    0x00000001107e2ad2 GSEventRunModal + 161
    24  UIKit                               0x000000010d4cff09 UIApplicationMain + 171
    25  Lettuce                             0x000000010a2c3f0f main + 111
    26  libdyld.dylib                       0x000000011012492d start + 1
    27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

经过一番调查,我发现了有问题的代码。它是我的 cardSwipedRight 方法中的一行,位于我的 draggableviewbackground class 中(此 class 用作一副牌,用于容纳所有卡片):

                [((settingsTableViewController *)([obj.superNavController.viewControllers[obj.profileNum] viewControllers][0])) askForPushNotifications];

这行代码位于检查这是否是 "sign me up for notifications card" 的 if 语句中。我感到困惑的是,为什么 xcode 将 filterPageView 与 askForPushNotifications 相关联。 filterPageView 不仅没有 askForPushNotifications 方法,而且我没有在 filterPageView 上向右滑动,也没有在我的应用程序崩溃时查看它。

无法识别的选择器发送到实例当您尝试调用对象上的方法但该方法不存在时出现(正确的术语是您已尝试发送一条名为 "askForPushNotifications" 的消息,但settingsTableViewController 不理解它,因为它 (askForPushNotifications) 不退出。我确定您的 settingsTableViewController 中可能没有该方法。请检查并告诉我是否对您有帮助。

此外,如果对象实际响应方法 askForPushNotifications,请检查 respondsToSelector。可能是

((settingsTableViewController *)([obj.superNavController.viewControllers[obj.profileNum] viewControllers][0]))

可能不是 SettingsTableViewController 类型。请检查

if [(((settingsTableViewController *)([obj.superNavController.viewControllers[obj.profileNum] viewControllers][0])) respondsToSelector:@selector(askForPushNotifications)]
{
    [((settingsTableViewController *)([obj.superNavController.viewControllers[obj.profileNum] viewControllers][0])) askForPushNotifications];
}

One receives an unrecognized selector exception when the object to which the selector is sent does not implement/respond to the sent selector.简单来说,如果 class A 没有实现某个方法,而有人试图在 class A 的对象上调用该方法,运行时会抛出无法识别的选择器异常。

当我们不确定对象是否会响应选择器时,我们应该进行安全检查以查看对象是否响应预期的选择器。

例如。

if ([*obj* respondsToSelector:@selector(*selector*)]) 
{
   [*obj* performSelector:*selector*];
}