• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Basic runtime function usage - objc_getClass
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Basic runtime function usage - objc_getClass


  • Subject: Re: Basic runtime function usage - objc_getClass
  • From: Sherm Pendley <email@hidden>
  • Date: Wed, 23 Nov 2005 12:23:19 -0500

On Nov 23, 2005, at 11:11 AM, Francis Derive wrote:

I beg the pardon of those who recently helped me, but now failed on this :

id myPointerToClass = (Class *) objc_getClass("MYobject");

This would be easier (see below) if you typed myPointerToClass differently. "id" is a typedef for "objc_object*", but in fact it can be either that or "objc_class*", either of which can be a message target. objc_getClass() returns a Class (not a Class*), but it's declared as id so that you can easily send messages to it. (Class, btw, is a typedef for objc_class*)


	Class myPointerToClass = (Class) objc_getClass("MYobject");

	if (myPointerToClass) {                                      // A
		NSLog(@"laClasse is registered");
		[...]
	}

In the Debugger Console I get this in A :

(gdb) print myPointerToClass
$1 = (struct objc_object *) 0x30038
-- feel good shape

The debugger is mistakenly reporting this as objc_object*, for the simple reason that that's what you told it, by typing myPointerToClass as id. But it's not - it's objc_class*.


(gdb) print myPointerToClass->isa
$2 = (struct objc_class *) 0x30068
-- feel good shape

For an instance, isa points to a class definition. But for a Class (i.e. for an objc_class struct), isa points to the metaclass, another objc_class.


(gdb) print myPointerToClass->isa->name
There is no member named name.
-- what ? why ? No way to understand !

I'm not certain whether a metaclass has a meaningful name. I can't think of any reason why it might need one; it's not really useful on its own, only as a part of the class with which it's registered.


If what you want is the name of the class, just declare myPointerToClass as the correct type, and look at its name member directly, for instance:

	if (myPointerToClass) {
		NSLog(@"%s is registered", myPointerToClass->name);
	}

sherm--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

_______________________________________________
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


  • Follow-Ups:
    • Re: Basic runtime function usage - objc_getClass
      • From: Francis Derive <email@hidden>
    • Re: Basic runtime function usage - objc_getClass
      • From: Damien Bobillot <email@hidden>
References: 
 >Basic runtime function usage - objc_getClass (From: Francis Derive <email@hidden>)

  • Prev by Date: Reverse value transformation with different class?
  • Next by Date: Mouse events and offscreen windows
  • Previous by thread: Basic runtime function usage - objc_getClass
  • Next by thread: Re: Basic runtime function usage - objc_getClass
  • Index(es):
    • Date
    • Thread