Re: object type testing
Re: object type testing
- Subject: Re: object type testing
- From: Ofri Wolfus <email@hidden>
- Date: Thu, 11 May 2006 00:12:33 +0300
If you need to know whether an unknown class conforms to a specific
protocol, you can use this code (taken from libFoundation and
modified a bit in mail):
struct objc_protocol_list *protos;
int i;
for (protos = aClass->protocols; protos != NULL; protos = protos-
>next) {
for (i = 0; i < protos->count; i++) {
if ([protos->list[i] conformsTo:@protocol(ProtocolForTesting)])
return YES; //Do something useful here
}
}
also, checking if a class responds to a selector is much easier:
BOOL respondsToSelector(Class aClass, SEL sel) {
return (class_getClassMethod(aClass, sel) ? YES : NO);
}
- Ofri
On 10/05/2006, at 23:29, Keith Ray wrote:
No, I'm not messaging an NSZombie object.
If I turn on NSZombies, then I end up messaging the NSZombie CLASS as
I enumerate through all the classes looking for the ones I'm
interested in.
Subclasses of NSProxy have also been a problem.
On 5/10/06, Chris Hanson <email@hidden> wrote:
On May 10, 2006, at 9:54 AM, Keith Ray wrote:
> I need to be able to determine if a class implements a certain
> protocol, but some of the classes I will be testing (and would
want to
> reject) are not subclasses of NSObject so they neither implement
> -conformsToProtocol: nor -respondsToSelector.
>
> NSZombie is one class that my code chokes on (so I have to keep
> NSZombies turned off), but there are sometimes others.
If you're messaging an NSZombie, that's a bug irrespective of whether
NSZombie responds to -conformsToProtocol: or -respondsToSelector:.
It means that you're messaging an object that has already been
deallocated.
What other classes that aren't subclasses of NSObject have you
encountered? (Are these in the wild, or in your own code?) NSProxy
is the principal one that I can think of from Cocoa, and it responds
to both of the above messages since it conforms to the NSObject
protocol.
-- Chris
--
C. Keith Ray
<http://homepage.mac.com/keithray/blog/index.html>
<http://homepage.mac.com/keithray/xpminifaq.html>
<http://homepage.mac.com/keithray/resume2.html>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40gmail.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden