1

In Objective-C, while creating any class, how do we decide whether we need to mark a method as Class method or Instance Method ?

I know the difference between the 2, but my question is how to decide the marking (+/-) for any method ?

1 Answer 1

7

+ denotes a class method, - denotes an instance method. You create class or instance methods where your application needs them. Should you actually know the difference between the two, and your application, then you should have no problems understanding when to use which.

I believe you don't know the differences in how they apply to your application, so here's a small primer:

  • You use a class method when you need to access some behaviour globally through all instances of that class. i.e., [[self class] someSpecialThing];
  • You also use a class method when you need a factory method; and
  • Everywhere else, you use an instance method.
Sign up to request clarification or add additional context in comments.

2 Comments

Good answer, though I believe it is a little astray-leading to say that messaging self's class is usually reserved for factory methods. In my opinion, any time you have functionality that is bound to a specific type, and yet doesn't care about any specific instance, you should be using a class method.
Sorry, poor wording on my part, I did not intend to imply that was good form, those were two separate thoughts. I will fix the wording.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.