• 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: ObjC Method naming
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ObjC Method naming


  • Subject: Re: ObjC Method naming
  • From: Chris Hanson <email@hidden>
  • Date: Mon, 28 May 2001 15:31:23 -0500

At 2:41 PM -0400 5/28/01, Pedrum Mohageri wrote:
Does this mean there is a method called set with two parameters width and
height?

[snip]

Or does it mean there a method called setWidth with two parameters, a
nameless one(default?) and height.

It means neither. The expression

[myRect setWidth:10.0 height:15.0];

means you're sending a message named setWidth:height: to the object myRect. The Objective-C runtime looks at the class of the receiver (myRect) and tries to determine if there are any methods it can invoke to handle that message. If there is an instance method in myRect's class (or its superclasses) named setWidth:height:, that's what the runtime will invoke. The parameters to the message and the method are interspersed with its name for readability. Thus it will be defined as something like

- (void)setWidth:(float)w height:(float)h
{ // method body here
}

and declared like so:

- (void)setWidth:(float)w height:(float)h;

The main new concept in Objective-C is that a bracketed expression is a "message" expression: A message is being sent to an object, be it a class (NSString) or an instance of a class (myRect). This results eventually in the invocation of a method, usually one with the same name as the message.[1] But even though they may share names, methods and messages are distinct things, just as in Smalltalk.

Another bit of trivia: Method and message names and called "selectors" in Objective-C and Smalltalk. You can manipulate selectors programmatically in Objective-C by specifying one in a @selector() expression. For instance, the expression

BOOL q = [myRect respondsToSelector:@selector(setWidth:height:)];

will set q to YES if myRect will *somehow* handle the message setWidth:height: and will set q to NO otherwise. Similarly,

IMP i = [myRect methodForSelector:@selector(setWidth:height:)];

will set i to an IMP (Objective-C function pointer) for the method that will handle the message setWidth:height: if one exists. (This is dangerous since applications in general cannot rely on this not to change, but it can be useful for optimization in very controlled circumstances.)

This is just barely scratching the surface of what Objective-C is capable of, but it's probably plenty for you to digest right now. :)

-- Chris

[1] This doesn't always have to be the case. If there's no method found which can handle the message, the Objective-C runtime will handle the failure very gracefully and give your object an opportunity to handle the unknown message. This means it can do things like parse the message and respond to it, or forward it to a remote object, or email the developer a "You idiot, you forgot to implement something!" message. See the definition of -forwardInvocation: in the class NSObject for more details about how this mechanism works.

--
Chris Hanson <email@hidden>
bDistributed.com: Making business distributed.
Personal email: <email@hidden>


References: 
 >ObjC Method naming (From: Pedrum Mohageri <email@hidden>)

  • Prev by Date: Message Queuing Question
  • Next by Date: Re: Exceptions
  • Previous by thread: Re: ObjC Method naming
  • Next by thread: Re: ObjC Method naming
  • Index(es):
    • Date
    • Thread