views:

24

answers:

1

Hi,

I am using both methods in my project, can you please let me know, which one is best for in which situation?

Regards, Sri

+1  A: 

Well, completely seriously, class methods should be specific to a class, and instance methods should be specific to an instance. Think about the method in question; does it depend on instance variables? Is it referring to a specific instance of the class in question? Or is it more general than that?

Frequently, class methods are convenient ways to return instances (for example, [NSColor redColor] is a handy class method for returning a common instance of NSColor). On the other hand, the instance method -greenComponent (which returns the green component of an RGB color) clearly needs to refer to a specific instance (if I asked you, "how much green is in color?" that wouldn't make sense. It's "how much green is in this color here?" that is a reasonable question).

You can also browse Apple's class references to get a better feel for which sorts of things are class methods (+) vs. instance methods (-).

andyvn22