1

I know that you send messages to objects using [ ], and it is then translated to objc_msgSend()which needs a pointer to object as its first argument and second argument needs a selector.

But what about messaging classes like [myClass doSomething]; It is not a pointer to object, so how does it work?

MiguelC
  • 298
  • 1
  • 3
  • 10
  • 4
    Classes are also objects. See http://stackoverflow.com/questions/5773054/are-classes-objects-in-objective-c/5773067#5773067 – iccir Oct 16 '13 at 10:15
  • @iccir: I know, but they aren't pointers to object. – MiguelC Oct 16 '13 at 10:23
  • 3
    Yes they are. An "object" is just a blob of memory with the first 4 (or 8) bytes containing an isa field (previously known as an isa pointer, but 64-bit ARM changed this). You can manually allocate memory, set the isa field directly, then message the "object". The Class has an isa that points to the metaclass, making it messagable via objc_msgSend – iccir Oct 16 '13 at 10:28
  • 1
    See http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html for a great diagram of this – iccir Oct 16 '13 at 10:36
  • @iccir: So where does the class which is a pointer to object, point. Sorry if I'm asking so much, I am so curious. Thanks for your links and answers man. – MiguelC Oct 16 '13 at 12:01
  • Can you rephrase the question? These were linked from my first comment, but maybe you didn't see them: http://cocoasamurai.blogspot.com/2010/01/understanding-objective-c-runtime.html http://www.cocoawithlove.com/2010/01/what-is-meta-class-in-objective-c.html I'm not sure if I can explain it better than the articles already do ;) – iccir Oct 16 '13 at 23:44
  • @iccir: Never mind my other question. What are the types of classes?? Are they type Class?? Cause I think it would be wrong. See my comment on pedro.m's answer. – MiguelC Oct 17 '13 at 00:50
  • @MiguelC: What do you mean by "types of classes"? A "type" ("compile-time type") applies to expressions and variables at compile time, so it makes no sense to talk about type of a class. `Class` is a pointer type that can hold a pointer to any class object, like how `id` is a pointer type that can hold a pointer to any object. You may also be talking about the class (the "runtime type") of an object. The class of a class object is a metaclass. Each class has a separate metaclass. – newacct Oct 17 '13 at 06:57

4 Answers4

1

Mike Ash has a great explanation of how objc_msgSend works: Friday Q&A 2012-11-16: Let's Build objc_msgSend.

John Topley
  • 113,588
  • 46
  • 195
  • 237
1

Actually, when you send a message to a class, you are actually sending that message to a class object, which behaves as any other object with some limitations.

From Apple's reference:

In Objective-C, a class is itself an object with an opaque type called Class. Classes can’t have properties defined using the declaration syntax shown earlier for instances, but they can receive messages.

Pedro Mancheno
  • 5,237
  • 4
  • 24
  • 31
1

I am assuming that myClass is (literally) the name of a class (though usually classes start with a capital letter; I'll assume this is a class that doesn't follow the naming convention).

There are two cases of the message sending syntax in Objective-C. One, which you referred to, is if the left side is an expression which evaluates to a pointer to an object; the message is sent to that object. The second, is if the left side is an identifier which is the name of a class, then the compiler will send the message to the class object for that class. More technically, the compiler will pass a pointer to the class object as the first argument to objc_msgSend(). This is possible since it is the compiler which arranges the structure and location of the class object for each class, so it knows the address of that class object.

Intuitively, you can think of [myClass doSomething]; as similar to

Class foo = objc_getClass("myClass");
[foo doSomething];

or

Class foo = NSClassFromString(@"myClass");
[foo doSomething];

except that it doesn't need to do a runtime lookup to get a pointer to the class object -- the compiler knows the pointer at compile time.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • Could you please explain more about the class object you talked about cause I think I'm having an understanding right now. By the way, thanks for your answer. – MiguelC Oct 17 '13 at 10:37
  • @MiguelC: The class object is the object that represents the class. Class methods are called by sending a message to the class object. Inside a class method, the `self` points to the class object it was called on. I'm not sure what in particular you want to know. – newacct Oct 18 '13 at 06:49
0

The runtime converts the message to a SEL then checks to see if that class responds to a message with that SEL including superclass inherited methods. If it responds it will perform the method.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55