0

There are dozens of questions about this on stackoverflow, but all conclude with the same answer that doesn't work in my case and doesn't apply to my situation... -all_load and/or -ObjC linker flags

I added a category on SKPhysicsContact called 'Concerns' with one method:

#import <SpriteKit/SpriteKit.h>

@interface SKPhysicsContact (Concerns)

- (BOOL)concerns:(SKSpriteNode *)sprite;

@end

.... implemented as:

#import "SKPhysicsContact+Concerns.h"

@implementation SKPhysicsContact (Concerns)

- (BOOL)concerns:(SKSpriteNode *)sprite
{
    return sprite ? [@[self.bodyA, self.bodyB] containsObject:sprite.physicsBody] : NO;
}

@end

The category does not exist in a separate static library. It's part of the app project.

The category file is targeted correctly and is listed in the Compile Sources build phase.

A symbol dump of the executable lists the category method correctly.

The header is #imported where the method will be used (obviously, or calling it would cause a compile-time error).

The -all_load/-ObjC fix suggested elsewhere does not fix the problem.

IMPORTANT: Categories on Foundation/UIKit classes added in the same way to the same project work fine.

I have tried unckecking/rechecking the target checkbox for the category's .m file... doesn't work.

This is Xcode 12.2, testing on iOS 14.2 and 15.1 on actual hardware and in the simulator.

Here's the console output:

2022-01-05 09:59:37.623224-0500 Jumpbread[40209:1514932] -[PKPhysicsContact concerns:]: unrecognized selector sent to instance 0x60000061ed00
2022-01-05 09:59:37.635814-0500 Jumpbread[40209:1514932] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PKPhysicsContact concerns:]: unrecognized selector sent to instance 0x60000061ed00'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000106f7baf2 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x0000000106e0be78 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000106f8a6f7 +[NSObject(NSObject) instanceMethodSignatureForSelector:] + 0
    3   CoreFoundation                      0x0000000106f80032 ___forwarding___ + 1489
    4   CoreFoundation                      0x0000000106f82068 _CF_forwarding_prep_0 + 120
    5   Jumpbread                           0x00000001064b7113 -[GameScene didBeginContact:] + 195
    6   PhysicsKit                          0x0000000113d26680 _ZN17PKContactListener13flushContactsEv + 390
    7   PhysicsKit                          0x0000000113d23cea -[PKPhysicsWorld stepWithTime:velocityIterations:positionIterations:] + 165
    8   SpriteKit                           0x0000000107727414 -[SKScene _update:] + 3071
    9   SpriteKit                           0x000000010774705d -[SKView _update:] + 950
    10  SpriteKit                           0x0000000107743624 __51-[SKView _vsyncRenderForTime:preRender:postRender:]_block_invoke.341 + 284
    11  SpriteKit                           0x0000000107742a10 -[SKView _vsyncRenderForTime:preRender:postRender:] + 528
    12  SpriteKit                           0x00000001077447e7 __29-[SKView setUpRenderCallback]_block_invoke + 203
    13  SpriteKit                           0x000000010778813f -[SKDisplayLink _callbackForNextFrame:] + 301
    14  QuartzCore                          0x000000010a5658a9 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 755
    15  QuartzCore                          0x000000010a64aa38 _ZL22display_timer_callbackP12__CFMachPortPvlS1_ + 639
    16  CoreFoundation                      0x0000000106eb79c8 __CFMachPortPerform + 157
    17  CoreFoundation                      0x0000000106eeac72 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    18  CoreFoundation                      0x0000000106eea013 __CFRunLoopDoSource1 + 614
    19  CoreFoundation                      0x0000000106ee44e2 __CFRunLoopRun + 2353
    20  CoreFoundation                      0x0000000106ee36c6 CFRunLoopRunSpecific + 567
    21  GraphicsServices                    0x000000010b139db3 GSEventRunModal + 139
    22  UIKitCore                           0x0000000115bab187 -[UIApplication _run] + 912
    23  UIKitCore                           0x0000000115bb0038 UIApplicationMain + 101
    24  Jumpbread                           0x00000001064b7a32 main + 114
    25  libdyld.dylib                       0x000000010bd1a409 start + 1
    26  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PKPhysicsContact concerns:]: unrecognized selector sent to instance 0x60000061ed00'
terminating with uncaught exception of type NSException
CoreSimulator 732.18.0.2 - Device: iPad Air (4th generation) (2257BC31-D7C7-4D0A-A979-FF6A1363C115) - Runtime: iOS 14.2 (18B79) - DeviceType: iPad Air (4th generation)
Jason L.
  • 41
  • 2
  • Can you paste the full text of the error message into your question? (A stack trace wouldn't hurt either.) – Phillip Mills Jan 05 '22 at 14:54
  • Lololol. After 2 hours of fiddling around with this, I still somhow didn't notice the "PK" prefix in the console output. Hat tip to PhillipMills for nudging me to add the console output and willeke for noticing the "PK" prefix and finding exactly what was going on based on that. @willeke Please break this out into an actual answer so that I can mark it – Jason L. Jan 05 '22 at 15:34
  • I was seeing that, and I guess that `PKPhysicsContact` doesn't inherit from `SKPhysicsContact`? – Larme Jan 05 '22 at 15:35
  • For anyone else who runs into this later, there are several ways to workaround this problem, but my quick fix was to make the category on NSObject rather than SKPhysicsContact. References to self in the category method have to be cast to SKPhysicsContact * and you have to remember to only call it on SKPhysicsContact/PKPhysicsContact instances, but it works. – Jason L. Jan 05 '22 at 15:47

0 Answers0