Re: GDB and display of object methods
Re: GDB and display of object methods
- Subject: Re: GDB and display of object methods
- From: Conrad Shultz <email@hidden>
- Date: Wed, 09 May 2012 11:46:05 -0700
On 5/9/12 10:57 AM, Jens Alfke wrote:
>
> On May 9, 2012, at 10:40 AM, Alex Zavatone wrote:
>
>> While po is nice to dump dict data, I seem to remember that there was
>> something to dump the methods of an object. Sadly my googles are
>> failing in this respect. Is there such a wondrous option or were the
>> fumes getting to my brain cell?
>
> Methods are code … how would you dump those? And the listing would be
> pretty long, because every regular class inherits a jillion methods from
> NSObject.
>
> If you mean *instance variables* instead, just do “print *object”.
It should be noted that there are runtime functions (e.g.
class_copyMethodList()) that allow inspection of methods.
But even the runtime functions are not going to capture everything you
might want. Because Objective-C uses dynamic message dispatch, there
is, in principle, no way to get a list of messages to which an object
will respond - only a list of methods that have been declared.
For example, try something like the following:
// main.m
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import "MyClass.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
MyClass *classInstance = [[MyClass alloc] init];
unsigned int methodCount;
class_copyMethodList([classInstance class], &methodCount);
NSLog(@"Beginning number of methods: %u", methodCount);
[classInstance fooMethod];
[classInstance barMethod];
[classInstance release];
class_copyMethodList([classInstance class], &methodCount);
NSLog(@"Ending number of methods: %u", methodCount);
}
return 0;
}
// MyClass.h
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
@end
// MyClass.m
#import "MyClass.h"
#import <objc/runtime.h>
void barMethodImplementation(id self, SEL _cmd) {
NSLog(@"Hello, I'm %@, responding to %@", self,
NSStringFromSelector(_cmd));
};
@implementation MyClass
- (void)fooMethod
{
}
+ (BOOL)resolveInstanceMethod:(SEL)sel {
if (sel == @selector(barMethod)) {
class_addMethod([self class], sel, (IMP)barMethodImplementation,
"v@:");
return YES;
}
else {
return [super resolveInstanceMethod:sel];
}
}
@end
--
Conrad Shultz
Synthetiq Solutions
www.synthetiqsolutions.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden