Re: Cocoa-dev Digest, Vol 3, Issue 420
Re: Cocoa-dev Digest, Vol 3, Issue 420
- Subject: Re: Cocoa-dev Digest, Vol 3, Issue 420
- From: John Sarkela <email@hidden>
- Date: Sun, 2 Apr 2006 14:16:44 -0700
Hi to everybody!!!
Could anyone of the gurus of this mailing list explain my this line
of code:
NSString *stringFinal = [NSString stringWithFormat:@"%@ tiene %d
letras",
[gEntradaDeTexto stringValue],
[stringDeEntrada length]];
More expecific, I don't understan why need introduce the class name
in the first part of de method call. I define a pointer to a variable
of NSString type and named -stringFinal- but when I make the method
call I need use NSString in the begining of the call, why?
Thank you very much in advance.
Best regards from Spain.
JuanC++
I would strongly recommend investigating the Squeak programming
environment. It is an open source implementation of the Smalltalk
programming language whose runtime was the inspiration for the
Objective C runtime. Your question relates to subtle notions of
objects, classes and metaclasses. Smalltalk actually provides deep
insight into how Objective C works. (BTW, there is a real hotbed of
Smalltalkers in Argentina. You may find a number of resources en
EspaƱol.)
<warning>The following discussion is intentionally imprecise. Some
descriptions of what occurs are gross simplifications of actual
runtime behavior. </warning>
The important distinction is of that between statically typed and
dynamically typed oo languages. Objective C incorporates the
Smalltalk model of dynamically typed objects. C++ borrows the Simula
model of virtual dispatch. In the first case *all* type information
is carried in the object. In the second case, dispatch information is
declared in the variable reference. Very often this is referred to as
late versus early binding. Binding is the process of associating a
message selector with a method implementation. Early binding
typically refers to compile time activities and late binding refers
to runtime activities.
Thus, C++ is statically typed and early bound. When you declare a
variable to be an object reference, the compiler associates a table
of virtual functions that provide implementations of the methods. At
compile time (early) the compiler can implement virtual dispatch as
an indexed lookup from a virtual function table.
OTOH, Objective C defers the binding of a message selector to a
method implementation until the very moment that a message gets sent
(as late as possible). To the runtime, all objects are of type id.
Every instance of id has an isa instance variable that is a reference
to the object representing its class. This class object has a set of
method dictionaries whose keys are message selectors and whose
objects are method implementations. So, when you send the message
"length" to a string, the runtime looks at the string object,
retrieves the class reference, looks up the key SEL(length) in the
method dictionary and invokes that method. When you send a message to
a class, for example "alloc", the runtime looks in the isa instance
variable of the class to get the reference to the object representing
its class (the metaclass) and looks up the key SEL(alloc) in its
method dictionary. This method is executed and it returns a value.
(Usually a new instance of the receiver.) (The interested reader
might question what the class of the class of the class is...)
When you declare a class in Objective C, you are really declaring a
pair of classes. The class and the class of the class aka metaclass.
Instance methods are declared with a preceding "-" and are added to
the method dictionary of the class. Class methods are declared with a
preceding "+" and are added to the method dictionary of the
metaclass. There is no special syntax for object creation, only plain
old vanilla message sends sent to class objects.
Let's investigate what your declaration means to the compiler and
then what it means to the runtime.
NSString *stringFinal = [NSString stringWithFormat:@"%@ tiene %d
letras", [gEntradaDeTexto stringValue], [stringDeEntrada length]];
The compiler will record the fact that the programmer intends to have
instances of NSString in the variable stringFinal. The compiler will
issue warnings if other message selectors are sent to the object
referred to by this variable. The initializer code will generate a
message send of "stringWithFormat:" to the class object with the name
NSString. Remember the only important syntax Objective C adds is
messageSend ::= [ <receiver> <message> ].
At runtime, when the initializer is evaluated, the message
"stringWithFormat:" is looked up in the method dictionary of the
metaclass of NSString. You need to put the NSString there to serve as
the reciever of the message "stringWithFormat:". In Objective C,
everything is an object, even classes. There is no special syntax for
creating objects. Objects are created by sending messages to objects.
Usually the object is a class object when I am creating a new instance.
Hope this helps... ;-}>
John Sarkela
[|] Knight of the Square Brackets
_______________________________________________
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