• 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 question on Methods
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie question on Methods


  • Subject: Re: Newbie question on Methods
  • From: Matt Neuburg <email@hidden>
  • Date: Sun, 12 Mar 2006 09:31:14 -0800
  • Thread-topic: Newbie question on Methods

On Sun, 12 Mar 2006 12:10:44 -0400, "Bobby B" <email@hidden> said:
>There is one concept I'm not understanding.  In the
>NSOpenPanelWithFileTypes example (on CocoaDev.com) there is the following code:
>
>NSArray *fileTypes = [NSArray arrayWithObject:@"td"];
>NSOpenPanel *oPanel = [NSOpenPanel openPanel];
>
>I understand that in the first line you are declaring an instance of
>the class (fileTypes) of the NSArray object.  But I don't understand
>the part that follows it.  Why do you send the message to the NSArray
>instead of to the fileTypes class?

Forgive me if I'm being overly petty, but it is well to use terms clearly,
so let's say at the outset, fileTypes is not a class. It's an instance - or
rather, it is to be an instance. It is to be an instance of the NSArray
class, and you are creating this instance.

To create an instance of a class, you send a message to that class.
Sometimes that message is alloc (followed by an init of some sort),
sometimes (as here) it is a "convenience constructor" (if that is the right
term). Your question seems to be about when to use which.

It would be perfectly possible, in this case, to say:

NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"td", nil];

However, then you'd have to remember to do memory management on fileTypes,
disposing of it yourself with release. Convenience constructors are often
more, uh, convenient. Don't get me wrong - there are lots of situations
where it is better to do your own memory management and use the alloc - init
dance (or where there is, indeed, no choice). But here you *do* have a
choice.

In the case of NSOpenPanel there is a further reason, I *think*, for using
the convenience constructor - it is initializing the resulting open panel
for you. This might be significant (but I'm not certain).

Finally, with regard to

NSString *s = @"yoho";

The answer is complicated, but on the whole you may regard the @"" syntax
as, in effect, a super-convenient convenience constructor: Cocoa makes the
instance, gives it a value, and manages the memory for you.

m.

--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>



 _______________________________________________
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

  • Prev by Date: Re: Sheet in a non-document-based Application
  • Next by Date: Re: good values for MDQueryBatchingParams
  • Previous by thread: Re: Newbie question on Methods
  • Next by thread: Re: Newbie question on Methods
  • Index(es):
    • Date
    • Thread