Reputation: 785
I am in the process of learning Objective C. Some rather different syntax; I don't see why they had to re-invent things, darn them.
I think I have this straight. An instance method is one where you have to create an instance of a class to use the methods. Proceeded by a minus sign. A class method is one where you DO NOT have to create a class instance. Proceeded by a plus sign.
So would it be appropriate to write a class that you don't intend to instantiate with methods that you can use where ever? Sort of like a general group of tool for your program? So long as I keep with the MVC methodology.
Upvotes: 0
Views: 214
Reputation: 21383
I am in the process of learning Objective C. Some rather different syntax; I don't see why they had to re-invent things, darn them.
Keep in mind that Objective-C was originally created in 1983, the same year as C++. So, it's not really a reinvention, at least of the languages you're likely used to. In fact it probably predates many of said languages. Objective-C is heavily inspired by Smalltalk, but I think you'd find Smalltalk syntax even more unfamiliar!
An instance method is one where you have to create an instance of a class to use the methods. Proceeded by a minus sign. A class method is one where you DO NOT have to create a class instance. Proceeded by a plus sign.
Yes, you essentially have that straight. Class methods, whose declaration/definition begins with a '+', can be called on a class itself instead of an instance of a class. In Objective-C, classes are themselves objects. Class methods can't access instance variables (because they're not called on an instance), but otherwise, they're regular methods. Instance methods may only be called on an instantiated instance of a class, and have access to that instance's internal state (ie. its instance variables).
So would it be appropriate to write a class that you don't intend to instantiate with methods that you can use where ever?
Sure, this is not unreasonable. Whether it's a great idea or not is another question. I think many times it's more appropriate to use regular functions rather than class methods for this sort of thing. Class methods are typically used for convenience methods (like factory methods), to configure global/default behavior of all instances of that class, and that sort of thing.
Upvotes: 8