I am using both methods in my project, can you please let me know, which one is best for in which situation?
Regards,
Sri
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 ofNSColor). 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 (-).