• 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: obj-c newb question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: obj-c newb question


  • Subject: Re: obj-c newb question
  • From: "Mike R. Manzano" <email@hidden>
  • Date: Tue, 4 Nov 2003 20:28:32 -0800

The difference between a class method (in other languages known as a "static" method) and instance methods is that class methods are called on classes, and instance methods are called on object instances (duh :).

Here is an example of why you would use a class method:

@interface SystemPrinters
{
}
+ (Printer *) defaultPrinter ;
@end

SystemPrinters would be a class that represents all printers on the system, and defaultPrinter would return the default printer for the system. Since you don't need more than one SystemPrinters objects in your program, there's no point in instantiating it, so you make its methods class methods. However, since you have multiple printers on your system, you'd want an object to represent each one of those, and so the Printer object would be instantiated, and the methods within it would be instance methods.

In Cocoa, lots of times class methods are used to instantiate different subclasses of a class (so, basically, the base class has, depending on the parameters that you pass to a class method, the intelligence to know what derived class is the proper one to actually instantiate).

Finally, class methods are used a lot to instantiate an object of the class that is pre-configured depending on the arguments passed to the class method. For example, NSString defines all of these class methods:

+ stringWithCString:
+ stringWithCString:length:
+ stringWithFormat:
+ localizedStringWithFormat:
+ stringWithContentsOfFile:
+ stringWithContentsOfURL:
+ stringWithCharacters:length:
+ string
+ stringWithString:
+ stringWithUTF8String:

All of them return an NSString object, but each is configured differently.

There are other uses of class methods (some dealing with serialization of threads and other more esoteric uses), but hopefully I've given you an idea when you should use them.

Regards,

Mike R. Manzano
email . alephx01 (at) mac (dot) com


On Nov 4, 2003, at 7:46 PM, Michael Stevenson wrote:

Thanks. Now I just realized what a dumb mistake that was... so, in what instances would you actually want to declare Class methods? What are they useful for? I cant map this idea to anything that I'm familiar with...

Thanks for the help..


On Nov 4, 2003, at 7:44 PM, Mike R. Manzano wrote:

device = [doop GetDefaultDevice];

Looks like you're trying to access a class method. Class methods are designated with a "+" instead of a "-". So,

+(AudioDeviceID)GetDefaultDevice;

in your declaration, and

+(AudioDeviceID)GetDefaultDevice {

in your implementation.


On Nov 4, 2003, at 4:40 PM, Michael Stevenson wrote:


main:
int main() {
id DefaultDevice;
AudioDeviceID device;
DefaultDevice = [[doop alloc] init];
printf("defaultdevice: 0x%x\n", (int)DefaultDevice);
device = [doop GetDefaultDevice];
printf("device id: 0x%x\n", (int)device);
return 0;
}//main
_______________________________________________
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.
_______________________________________________
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.
_______________________________________________
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: 
 >obj-c newb question (From: Michael Stevenson <email@hidden>)
 >Re: obj-c newb question (From: "Mike R. Manzano" <email@hidden>)
 >Re: obj-c newb question (From: Michael Stevenson <email@hidden>)

  • Prev by Date: Apple Script Studio
  • Next by Date: Re: Why won't Cocoa app run under Xcode?
  • Previous by thread: Re: obj-c newb question
  • Next by thread: Re: obj-c newb question
  • Index(es):
    • Date
    • Thread