Jump to content

Objective C definition help

3 posts in this topic

Recommended Posts

I am a little bit stuck on Class methods.


Can anyone provide me an example?


Here is what I am thinking it is:


Its kinda like Nouns and Verbs


For example:


[cat feeding]; // feed the cats


Link to comment
Share on other sites

Took me a while to understand the concept of class methods but from what I gathered from the below link is that you use a class method can't make changes via instance variables but only using class variables that you have defined.  Class methods also can be called without instanciating the class like so

MyClass *myObject = [[MyClass alloc] init];

I mainly use them when just solely dealing with class variables with no ivars included.  I use instance methods when I want to change things that aren't directly related to the class or for temporary changes.




Link to comment
Share on other sites

Class methods are most often used as factory methods that create new instances of that class. Related is the Singleton pattern in which only one instance of the object type is allowed to be instantiated. They can also be used just for organization of methods that would otherwise just be regular functions into classes.


In this pseudocode example below:


Window *myWindow = [Window topWindow];



That would call the class method "topWindow" in the "Window" class and presumably return a pointer to the instance of Window that is the topmost window. It is a class method because there is only one topmost window and this returns an instance of that one.


In comparison, an instance method would work like this:


[myWindow sendToBack];


That tells myWindow, a specific instance of Window to rearrange itself to the back of all the windows. It can't be a class method because it has to refer to just one Window.



Hopefully that makes sense.

Link to comment
Share on other sites


  • Create New...