0

I create a runtime class and then swizzle an instance to be an instance of that new class.

MyClass *object = [MyClass new];
Class subclass = objc_allocateClassPair([MyClass class], "MyClass_RuntimeClass", 0);
objc_registerClassPair(subclass);
object_setClass(object, subclass);

This works fine. I can call methods and set properties defined on MyClass after swizzling.

The only problem is that the debugger no longer shows the properties of the object.

Apple seem to have overcome this issue with their KVO runtime classes.

I tried adding the properties on the new class using class_addProperty, but this fails as they are already defined.

Is there something I am missing?

MarcB
  • 1
  • 1

1 Answers1

0

Apple overrides the class method to call the super's implementation, thus actually returning the original class (in your case MyClass). If the debugger uses the class property to decide what properties to show, it will be confused, since it does not have a definition for MyClass_RuntimeClass.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • Thanks for the response, I gave that a go and it didn't work :( I realised that if you call object_getClass on a KVO'd instance it returns the KVO subclass. If you then call class_copyPropertyList on that KVO subclass it doesn't return any properties. So I think Apple is doing something subtle to trick the debugger. To be honest I don't really use the debugger much as I practise TDD. – MarcB Apr 19 '14 at 09:43
  • KVOd subclasses actually implement the properties (wrappers) to notify the observers. – Léo Natan Apr 19 '14 at 09:55
  • Leo thanks for your help. I realised that my problem was due to the fact I had overrided the implementation of forwardInvocation: in my class. When I removed that I now see everything in the debugger. Plus overriding the class method as you said, gives me what I wanted. – MarcB Apr 19 '14 at 11:09