Quote:
Originally Posted by TheFlyingDutchman
But that's it? There should be another reason. Please explain better.
|
Do you mean like when you'd use one over the other?
It all boils down to object oriented programming, which is a topic you're going to need to review. Google is your friend - my bookshelf is more than 12 years old at this point
In a nutshell, a class method is something that you would not need a specific instantiation of an object in order to invoke. The most useful examples are in NSString - you can call, for example, stringWithFormat to allocate, initialize, and auto-release a string object for you in one fell swoop. They tend to be "meta" methods - you're not doing something to the object, you're doing something about or with the object in a more general sense.
An instance method performs some action on a specific instance (see how that works!) of an object. Using NSString again, the method lowercaseString converts a string to its lowercase form. It doesn't convert every string everywhere in the universe - it only acts on that one specific instance of a string.
You'll find class methods used frequently with singletons - such as the currentDevice method of UIDevice. There's only one device - the one your app is running on! You can't initialize a new UIDevice object because that would be completely silly. Lookup "singleton pattern" on Google.
Because class methods are called without a specific instance attached to them you can't reference any data held by that object. Unless it's held by ALL instances (aka static member), but if you're having trouble with +/- I'd save static members for another day.
Most of the time you'll be creating instance methods. If you have a CKittyCat object and create a method called meow, you'd want that method an instance method (-). Then you could direct an individual kitty cat object to meow for you. I can't think of anything on a CKittyCat that would make sense to have as a class method (alloc, etc. don't count!).
Note: this is a rough sketch only. There's plenty of stuff left out (like an entire discussion of vtables, Obj-C messaging, polymorphism, inheritance, etc.). Hit your bookstores & libraries for object-oriented programming books - it won't really matter if they're Java, C++, C#, Objective-C, Smalltalk, or what have you. Try to find ones that are more overview than language specific, which is why I mention looking at actual paper vs. Amazon searching.