Can't add category method in SKPhysicsWorld

This:

@interface SKPhysicsWorld (Test)
- (NSInteger)test;
@end

@implementation SKPhysicsWorld (Test)
- (NSInteger)test {
    return 0;
}
@end

Gives me this:

-[PKPhysicsWorld test]: unrecognized selector sent to instance 0x...


Why is that?

Replies

That tends to suggest the category isn't getting loaded. Where is this @implementation (in a static library, framework, main app target)? What does the call site look like?


It's also possible that Sprite Kit plays some run-time shenanigans with its classes, so that extending some classes this way isn't possible.

Main target. Conventional way, in SKPhysicsWorld+CategoryName.h(.m) files. Calls in SKScene subclass -update method.

That strange.


No doubt SKPhysicsWorld is a class cluster, but that never was the constraint, am I right?

Duh, I assumed that PKPhysicsWorld was a typo, but I just tried adding the category and found out this is the class name. It turns out that Sprite Kit is playing shenanigans with classes:


(lldb) po [self.physicsWorld className]
PKPhysicsWorld

(lldb) po self.physicsWorld.superclass
NSObject

(lldb) p [self.physicsWorld isKindOfClass: [SKPhysicsWorld class]]
(char) $1 = '\x01'


So it's not a SKPhysicsWorld class cluster, and PKPhysicsWorld is not a subclass of SKPhysicsWorld, but it does pose as a SKPhysicsWorld!


If you must add the method, you might be able to do so by manipulating the Obj-C runtime metadata directly, but it doesn't seem like a good way, going forward.

Thanks. I will use good old free functions that takes SKPhysicsWorld instance as parameter.


Found this old thread:

https://forums.developer.apple.com/message/82313


Apple Staff answer:

"We see your issue and we'll investigate if there is a way to make category mixins work in the future."

Looks like this will never happens.