Determining if a method is a class or instance method
Determining if a method is a class or instance method
- Subject: Determining if a method is a class or instance method
- From: Prachi Gauriar <email@hidden>
- Date: Sun, 11 Jul 2004 22:19:47 -0500
Today I wanted to write functions that could pretty print a selector or
method name, e.g.
Pretty printed selector: -mySelector:
Pretty printed method name: -[ClassOfReceiver mySelector:]
While the latter can be done easily with GCC's __PRETTY_FUNCTION__
variable, you can't do the former as easily. Obviously there's
NSStringFromSelector(), but you don't get the - or + depending on if
it's a class or instance method. I remember this came up before long
ago <
http://cocoa.mamasam.com/COCOADEV/2003/07/2/69075.php>, so I
thought I'd share my solution. Here are my functions:
NSString *PGPrettySelector(id receiver, SEL selector)
{
char methodType = [receiver isMemberOfClass:[receiver class]] ? '-'
: '+';
return [NSString stringWithFormat:@"%c%@", methodType,
NSStringFromSelector(selector)];
}
NSString *PGPrettyMethodName(id receiver, SEL selector)
{
char methodType = [receiver isMemberOfClass:[receiver class]] ? '-'
: '+';
return [NSString stringWithFormat:@"%c[%@ %@]", methodType,
NSStringFromClass([receiver class]),
NSStringFromSelector(selector)];
}
These both rely on the fact that class objects are the only objects
that are not members of their own class, because as the
isMemberOfClass: documentation says, "Class objects are not 'members
of' any class."
Anyway, I hope that helps someone out there, either now or in the
future.
-Prachi
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.