views:

91

answers:

4

I've seen many other questions on this same topic but they're not very clear to me, someone new to Objective-C.

I need a plain english explanation. 'Coder speak' is much too difficult for me to understand at this point in my learning.

+2  A: 

An instance method or variable applies to that instance. A class method (classes in Objective-C don't have variables, though they can be simulated in various ways) applies to the whole class.

Consider the quintessential Dog class, which derives from the Mammal class (and so on up the tree of life.) A particular dog has a name and a collar and an owner--those are its properties. A particular dog may -bark or -chaseBall or -buryBoneInBackyard--those are its methods.

The Dog class, on the other hand, has different methods. The Dog class has a +globalPopulation and may instantiate itself with a +dogWithDNA: factory method. The Dog class will have an +isExtinct method indicating whether the species as a whole is extinct (it's not, of course.)

In short: class methods affect the entire class, while instance methods affect a particular instance of a class.

Jonathan Grynspan
I think I am starting to get it but it's still a little fuzzy. Can you provide me with a coded example?
Chris Amini
I've been reading and I am assuming this is similar to PHP's public and private functions?
Chris Amini
No, public/private tells you who can access a function. The class/instance distinction tells you what responds to the message: the class itself, or an instance of the class. Does the species `Dog` respond to it? It's a class method. Does a particular dog named Scruffy who lives down the street respond to the method? It's an instance method.
Jonathan Grynspan
Still confused.
Chris Amini
Are you aware of the difference between a class and an instance?
Jonathan Grynspan
Nope lol. I am relatively new to objective programming in itself. I've done a lot of PHP and Ruby stuff but usually avoided OOP.
Chris Amini
The biology metaphor again: a class is the same as a species. "Duck" or "Human" or "Dog" is a class. "Donald" is an instance of "Duck", "Albert Einstein" is an instance of "Human", and "Lassie" is an instance of "Dog." If you don't already know that distinction, the distinction between class and instance methods is going to be meaningless to you.
Jonathan Grynspan
A: 

Instance variables (ivars) and instance methods exist on each instance. There is one ivar per instance. Instance methods can not be called on classes.

Class variables^ and class methods do not not exist on instances, they exist on the class. That means that there will only ever be one class variable in the entire application regardless of how many instances are created. Class methods can be called without an instance*, so they kind of act like normal C functions. Because class methods are not attached to an instance, class methods can not access ivars.

^ Objective-C doesn't have class variables per se. "Class variables" are effectively static global variables in C.

* technically, a class is an instance, so class methods are actually instance methods in a sense.

Tom Dalling
+2  A: 

First, Objective-C does not have class variables. There are things that act sorts like class variables modally, but they aren't true class variables (see "static variables").

In Objective-C, every class is effectively an instance of a class. Thus, a class method is simply a method that applies to the class. They can be inherited and overridden.

bbum
A: 

A class is like a mold for something, that you can fill with plaster.

So a class level method is something that you can see and reach and use, without ever having to make a single object.

An instance is like pouring plaster into the mold and getting something out. You stamp out as many as you need; an instance variable then is a place on that object to hold something, and an instance method is something you can do with only that single object, not all of them.

Kendall Helmstetter Gelner
So if I've got you, an instance method is tied to a specific object and a class object can be used anywhere?
Chris Amini
A class method can be used from anywhere, just to be clear there is not really such a thing as a class object...
Kendall Helmstetter Gelner