关于 ios:Terminating app due to uncaught exception \\’NSInvalidArgumentException\\’ unrecognized selector sent to instance Objective-C Singleton

Terminating app due to uncaught exception 'NSInvalidArgumentException' unrecognized selector sent to instance Objective-C Singleton

我正在调试我们的应用程序中仍在使用的非常旧的代码,因为它在我们的应用程序中引起了崩溃。该错误与 Objective-C 相关:

崩溃:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TrackService exit]: unrecognized selector sent to instance 0x281cb4380'

崩溃的代码:

1
2
3
4
5
6
7
8
@implementation TrackService

+ (TrackService *)sharedService
{
    DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
        return [[self alloc] init];
    });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 GCD Singleton
 */


#define DEFINE_SHARED_INSTANCE_USING_BLOCK(block) \\
static dispatch_once_t pred = 0; \\
__strong static id _sharedObject = nil; \\
dispatch_once(&pred, ^{ \\
_sharedObject = block(); \\
}); \\
\\
return _sharedObject;

#endif

堆栈跟踪:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2020-02-11 09:49:35.737492+0200 TestApp[79860:6925721] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TrackService exit]: unrecognized selector sent to instance 0x600001dc6380'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010ee3b1bb __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x000000010e3d9735 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010ee59f44 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000010ee3fed6 ___forwarding___ + 1446
    4   CoreFoundation                      0x000000010ee41da8 _CF_forwarding_prep_0 + 120
    5   TestApp               0x00000001029b6575 $s2017fromNavControllerSbSS_SDySSypGSgSo012UINavigationK0CSgtFySSSgcfU13_ + 165
    6   TestApp                0x00000001029b663f $sSSSgIegg_So8NSStringCSgIeyBy_TR + 175
    7   TestApp                0x000000010249decf -[TrackViewController exit:] + 1551
    8   TestApp                0x000000010249e275 __36-[TrackViewController exit:]_block_invoke + 757
    9   TestApp                0x000000010246834c -[TrackViewController pickAction:] + 220
    10  UIKitCore                           0x0000000115a2cecb -[UIApplication sendAction:to:from:forEvent:] + 83
    11  UIKitCore                           0x000000011516d95b __45-[_UIButtonBarTargetAction _invoke:forEvent:]_block_invoke + 154
    12  UIKitCore                           0x000000011516d894 -[_UIButtonBarTargetAction _invoke:forEvent:] + 152
    13  UIKitCore                           0x0000000115a2cecb -[UIApplication sendAction:to:from:forEvent:] + 83
    14  UIKitCore                           0x00000001154680bd -[UIControl sendAction:to:forEvent:] + 67
    15  UIKitCore                           0x00000001154683da -[UIControl _sendActionsForEvents:withEvent:] + 450
    16  UIKitCore                           0x000000011546731e -[UIControl touchesEnded:withEvent:] + 583
    17  UIKitCore                           0x0000000115a680a4 -[UIWindow _sendTouchesForEvent:] + 2729
    18  UIKitCore                           0x0000000115a697a0 -[UIWindow sendEvent:] + 4080
    19  UIKitCore                           0x0000000115a47394 -[UIApplication sendEvent:] + 352
    20  UIKitCore                           0x0000000115b1c5a9 __dispatchPreprocessedEventFromEventQueue + 3054
    21  UIKitCore                           0x0000000115b1f1cb __handleEventQueueInternal + 5948
    22  CoreFoundation                      0x000000010eda0721 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    23  CoreFoundation                      0x000000010ed9ff93 __CFRunLoopDoSources0 + 243
    24  CoreFoundation                      0x000000010ed9a63f __CFRunLoopRun + 1263
    25  CoreFoundation                      0x000000010ed99e11 CFRunLoopRunSpecific + 625
    26  GraphicsServices                    0x00000001141fc1dd GSEventRunModal + 62
    27  UIKitCore                           0x0000000115a2b81d UIApplicationMain + 140
    28  TestApp                0x000000010262672b main + 75
    29  libdyld.dylib                       0x0000000110ab5575 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

代码在这一行崩溃TrackService.shared().exit()


问题是没有实现 exit 函数,即使它是在 .h 文件中定义的。


我认为您需要将 return [[self alloc] init]; 更改为 return [[TrackService alloc] init];,但甚至可能重写它,使其不再使用#define,如下所示。

1
2
3
4
5
6
7
8
9
+ (TrackService *)sharedService
{
    static TrackService * t;
    static dispatch_once_t onceToken;

    dispatch_once ( & onceToken, ^ { t = [[TrackService alloc] init]; } );

    return t;
}