Re: Determining if a method is a class or instance method
Re: Determining if a method is a class or instance method
- Subject: Re: Determining if a method is a class or instance method
- From: Jérôme Laurens <email@hidden>
- Date: Fri, 16 Jul 2004 16:31:05 +0200
- Resent-date: Fri, 16 Jul 2004 16:34:22 +0200
- Resent-from: Jérôme Laurens <email@hidden>
- Resent-message-id: <email@hidden>
- Resent-to: email@hidden
Le 12 juil. 04, ` 05:19, Prachi Gauriar a icrit :
>
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
I personnaly use the following function to make the difference between
class objects and instances.
This is very comfortable because it does not send any message to the
argument, so it can be used in +load
without +initialize being performed. However, it does not work for
NSObject.
BOOL iTMIsClassObject(id O)
{
struct objc_class * C;
C = (struct objc_class *)O;
return strcmp(object_getClassName(C), object_getClassName(C->isa))
!= 0;
}
_______________________________________________
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.