• 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: Nelson Santos <email@hidden>
  • Date: Sun, 12 Mar 2006 11:29:42 -0800

Hi Bobby,

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?  And the same question follows for
the NSOpenPanel right below it.



First, let me clear up something that I think you are confused about. "fileTypes" is not a class. NSArray is the class. "fileTypes" is an instance (an object) of class NSArray. Now, with that being said, there are two ways to create an instance of an object. You can either allocate memory for the object directly using the alloc/init methods, or you can call the class's convenience methods (such as arrayWithObject) that does that for you. The difference is who own the object. Calling the alloc/init methods to create your object means that your code owns the resulting object and thus means that at some point you have to release the memory for that object yourself (using the release method). Calling one of the convenience methods means that the object is "autoreleased" and will therefore be released from memory automatically in the next message loop pass.


Of course, you can take ownership of an object that you have created using the convenience methods by calling the retain method on it which would mean, once again, you would need to release it yourself at some point.

Creating the object using one way or the other depends on the situation. One way to decide is this: If you only need the array temporarily, use the convenience methods. If you need the array as part of your model object, use the alloc/init methods. Another way to decide is this: If there is a convenience method that does what you need, such as create an array with 5 objects right off the bat, then you can call the arrayWithObjects method and pass it 5 objects to fill the array with. Then you can either retain the resulting object or not depending on how long you need to keep the object.


I was using the following code for the NSOpenPanel:

NSOpenPanel * op;
op = [[NSOpenPanel alloc] init];

How is that functionally different from:

NSOpenPanel *oPanel = [NSOpenPanel openPanel];


NSOpenPanel is different. This is a "singleton" class which means that one and only one object of this type can exist at any point in your program. Therefore you should not (and maybe can not) start allocating memory for an object of this class all over the place. The object will be created the first time you call the "openPanel" method and will remain in existence throughout the life of your program. Calling "openPanel" after that point will simply return that same object again.


Let me give you some good advice. Cocoa programmers are never without their documentation. It's impossible to remember every little thing about Cocoa. If you are not sure about how to instantiate a class or call a method, just option double-click on the class or method name and read up about it. Whether you are a newbie or an experienced Cocoa programmer, this is what you should always do.


Is it something regarding object methods and instance methods?


I think what you mean to say is CLASS methods and instance methods. Objects and Instances are synonymous. Whenever you want to instantiate a class, you ALWAYS have to call a class method (such as alloc, or arrayWithObject) because you don't have an object yet and thus cannot call any instance methods on it.



To
further obfuscate my question, when do you have to do the [[Receiver
alloc] init] method?  I notice when you declare a string : NSString *
myString =@"This is my string"; you don't have to run [[NSString
alloc] init]; on it.  Why is this?


In this case "@" is a special Objective-C keyword. It means "create a static instance of a string object". So you are indeed creating valid Cocoa object this way. Nothing stops you from using alloc/init instead (and at some point release since you are now responsible for the object).


I hope I've been helpful.

Nelson Santos



_______________________________________________
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


  • Follow-Ups:
    • Re: Newbie question on Methods
      • From: Daniel J Farrell <email@hidden>
References: 
 >Newbie question on Methods (From: "Bobby B" <email@hidden>)

  • Prev by Date: Re: Advice On Where To Start
  • Next by Date: Re: Code to Run on Application Load
  • Previous by thread: Re: Newbie question on Methods
  • Next by thread: Re: Newbie question on Methods
  • Index(es):
    • Date
    • Thread