I am writing an app a little bit like class-dump, as an
educational exercise in (a) Mac OS X Cocoa programming and (b) to
learn what things are actually available in the Cocoa API - it's
an interesting task.
I've struck a problem however, with the discovery of formal
protocols. Quite likely, I am doing something silly.
My understanding of the obj-c meta information is such:
- - a class that implements a formal protocol, by declaring it in
the header and implementing that protocols methods - should have a
Class structure representing that [implemented] protocol in it's
Class->protocols list.
e.g.
@interface MyClass : NSObject < TheProtocol >
...
- - the obj-c 'Class' structure contains a list of implemented
[formal] protocols, reachable by traversing the Class->protocols
pointer.
- - protocols are simply 'struct objc_object' structures, i.e.
classes (once one traverses the objc_object->isa relationship)
My problem is that all the protocols I am traversing are called
'Protocol', and I can't seem to find the test protocol that I've
implemented to check this.
A protocol is described by an object, not a class. Every protocol
is an instance of the class Protocol, so [[prot class] name] is
always Protocol. Instead, you should get the name of the protocol
from the Protocol object itself. Class Protocol is described in
objc/Protocol.h; it includes a -name method.
I will give this a shot and let the list know how it goes. </hiatus>
I gave it a shot - and yes, this works perfectly - thank you. Lord
knows what I was thinking when I wrote the original code - the
Protocol.h header had escaped me too - so that didn't help.
id prot = *(prot_list->list + index);
Class class_of_protocol = [prot class];
This looks suspicious, by the way. [NSObject class]->super_class
should be nil. I think your loop works, but it probably doesn't
work the way you think it does. A loop like this is simpler:
Yeah, I didn't like it much either - I think I copied this over from
some ->isa checking... which does indeed require this check, but I
double-checked in the debugger and super_class will simply go zero
when one gets to the top of the class chain.
Greg - thanks - this little tit-bit gave me enough insight to finish
what I started, and now I've got a nice little tool to explore the
API with.
thanks - take care,
john clayton
for (theClass = objc_getClass(className);
theClass != nil;
theClass = theClass->super_class)
{
// do stuff with theClass
}
iD8DBQFD0/UCp5OOqU+VYFgRAvU9AJ9c9rMJwshHsdfPwpTpiDhcSOfAMACgpvio
Q+hf+sApxkLXiFp/OlHY9XI=
=bJ6h
-----END PGP SIGNATURE-----
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden