Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: finding protocols for a given class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: finding protocols for a given class



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On 21 Jan 2006, at 23:34, Greg Parker wrote:

On Jan 21, 2006, at 1:07 AM, John Clayton wrote:

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];

NSString *protName = [NSString stringWithCString:class_of_protocol->name];

Try this instead: #include <objc/Protocol.h> // if necessary Protocol *prot = (Protocol *)prot_list->list[index]; NSString *protName = [NSString stringWithCString:[prot name]];


bool isNSObject = theClass == theClass->super_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 }


-- Greg Parker email@hidden Runtime Wrangler



-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (Darwin)

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


References: 
 >finding protocols for a given class (From: John Clayton <email@hidden>)
 >Re: finding protocols for a given class (From: Greg Parker <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.