Rule of thumb: If you have no reason to make it a class method, make it an instance method.
Reasons:
1. Most methods in most classes you'll every write will be instance methods. Learn how to make those effectively, and the reason for using a class method will become clearer.
2. It's easier to subclass, use protocols, categories, and so on with instance methods.
3. If it doesn't affect the class as a whole, or all instances of the class in the same way, then it shouldn't be a class method. Creational methods (e.g. see the stringWithXXX methods of NSString) should normally be class methods, since they affect all instances of the class in the same way (i.e. all instances are created from the same creational method in the same way).
You make it sound like class methods are evil and bad. They are not, and they have their uses.
I would explain things differently.
Instance methods are sent to an INSTANCE of a class. An instance of a class is a concrete thing. It has state data that persists for the life of the object. When you send a message to an object, it can use that state data to do things, and it can remember information for later.
Class methods are sent to the class as a whole. A class is a more abstract concept. Classes do not save state data, at least in Objective C (In other languages like C++, there are class variables that belong to the entire class, but Objective C does not have class variables.)
An analogy. Imagine a car factory:
The car factory is the class. The car class has a class method buildCar. The buildCar method takes parameters that tell what color the car should be, how big the engine should be, what options it should have, etc. You sent the car factory (the class) a buildCar message, and it creates and returns an instance of that class (the car factory builds you a car and gives it to you.)
Once you've created a car, that particular car has state variables that store things like the channel on the radio, the amount of gas left in the tank, the milage ("kilometerage"?) on the odometer, the wear level on the brakes, etc. Different cars will have different values for those variables.
You can ask an individual car how much gas is left in it's tank. (instance method.) Car A might have a full tank, and car B might be nearly empty.
However, it would not make sense to ask the car factory how much gas it has in the tank. A car factory doesn't have a gas tank. However, you could ask a car factory about the types of cars that it can build and the options for those cars. That is static information that always holds true for that car factory.
In addition to "factory methods" (e.g. build me a car), you can use class methods like you use functions in a procedural language. A class method is self-contained. You pass all the parameters into the method that you need in order to do whatever task is required, and you get back a single result (or no result for a void class method.) Once the class method is done executing, there is no state data left to record what happened.
You could write a math library as class methods, or you could write it as a C function.
For example, you could write a sine function as a C function:
Code:
float sine(float theta)
{
return sin(theta);
}
And you'd call it like this:
Or, you could create a Math class, and define a sine class method:
Code:
@interface Math: NSObject;
+ (float) sine: (float) theta;
@end
Code:
@implementation Math
+ (float) sine: (float) theta;
{
return sin(theta);
}
@end
You'd call the class method like this:
When you call a class method, you put the name of the class as the first thing inside the brackets, and then any parameters. That tells you that the message gets sent to the class, not an instance of the class.