• 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: Newbie Questions About Methods and Messages
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie Questions About Methods and Messages


  • Subject: Re: Newbie Questions About Methods and Messages
  • From: Ondra Cada <email@hidden>
  • Date: Sat, 10 Aug 2002 00:04:16 +0200

On Friday, August 9, 2002, at 10:45 , Brendon Bruns wrote:

I am a newbie to the world of Cocoa so please don't flame me for asking
simple questions.

At risk of being (just again) marked as the rudest person here, I can't resist pointing out that nobody was ever flamed for asking simple questions. The ticket to flame always was a question based on utter laziness to check docs first -- an excellent example which really occurred here was "Anybody tell me, how to find a string length"?

And let me also point out that your question is a *very good one*, one which aims directly at something quite difficult to grasp for anyone who is used to statical languages only.

Anyway, to start off with, methods and messages both appear that they can
both return information to whomever send them. If they return nothing, they
return void. Am I correct so far?

No ;)

The thing is that methods and messages are not just different kinds of procedures (as your formulation seems to imply). Messages are just that, *messages*. Without any important mistake, you can imagine a message is just a piece of paper on which the message name (like "length" or "objectAtIndex:") is written.

A piece of code like "[foo length]" means:

(i) prepare this piece of paper, write the name there, and adds any arguments;
(ii) send it to the object "foo";
(iii) wait what "foo" will do...

(iv) "foo" gets the paper and reads the message;
(v) it finds there is a method of the same name as the message has got...
(vi) ... so the method is called, as simply as any normal plain C function.

(vii) the return value of the method is "returned from the message".

Well, my main question is, what is the point, really, of having a method
returning data when a message could also do it? You have to invoke a method
into action by a message in order for it to do anything, so why would the
method need to return anything? Or is the methods data passed onto the
message which brings it back? I am still quite baffled by this.

In principle, the message ("objectAtIndex:") and the argument ("3") are packed together. The "package" is sent to the target object. It tries to select an appropriate method; if successful, the method -- it is just a plain function -- is called with the argument(s) given. If the method returned anything, it is sent back, so that -- from the POV of the caller -- it can be "returned from the message".

In other words: a message and a method are just two parts of one thing: a message is sent to invoke a method. From the other side, if a method is invoked, it is as a result of receiving a message.

Any "message arguments" are not in fact arguments of the message: they are,
actually, arguments of the method which would eventually become called as a result of the message sending. Analogically, message can't ever return anything -- what seems so is what the method (called as a result...) actually returned.

In practice it is slighlty more difficult, but this is the gist of it.

Another question, a class, when executed, creates an object instance of that
class. Correct?

If in "executing class" you mean "sending a message to a class" (which, as we know from the above paragraphs, would eventually mean "executing some class method"), then roughly correct.

(More precisely, class methods -- triggered by messages sent to the class -- can do just anything; creation of instances is only a special case.)

Well, I believe there is a way to create objects without
using a class...

There is indeed, but do forget it until you become MUCH more experienced with ObjC. For awhile, let's forget it and pretend there is *no* other way to create instances than via class methods.

or that is how I have interpreted it. Or, do you always
need to have a class declare an instance of itself to make an object, or can
it be done without the class?

This I can't completely follow.

(a) to make an instance, you have always first to describe it -- which is (almost) the same as "creating its class";
(b) class does not and need not to "declare an instance of itself".

Perhaps you might elaborate a bit this last question?

And, one important thing (last but definitely not least): an "object" is anything a message can be sent to: either a class, or an instance. This is important to know -- there's too many people misguided by C++ or similar catastrophes to mess this up, saying "object" (or even "class") when they really meant "instance".
---
Ondra Hada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: 
 >Newbie Questions About Methods and Messages (From: Brendon Bruns <email@hidden>)

  • Prev by Date: Re: Strings from UUID
  • Next by Date: Re: Strings from UUID
  • Previous by thread: Re: Newbie Questions About Methods and Messages
  • Next by thread: NSButtonCell, Focus and NSTableView
  • Index(es):
    • Date
    • Thread