PDA

View Full Version : Understanding issues with Class Methods?




q64ceo
Jul 6, 2013, 03:15 PM
I am a little bit stuck on Class methods. I am completely new to Objective-C.

Can anyone provide me an example?

Here my best guess:

Its kinda like Nouns and Verbs

For example:

[cat feeding]; // feed the cats

Thank you for your time.



ArtOfWarfare
Jul 6, 2013, 11:45 PM
What you're describing sounds more like an instance method than a class method.

Class methods do one of two things:
1 - Generate a new instance (IE: alloc, stringWithFormat:, ...)
2 - Serve as a stateless utility method that would probably be better off as a plain old C function.

Instance methods are stately. Mostly you want to get and or set things based on the state of an instance of an object.

Is that clear or is it too abstract?

I feel like OOP is fairly complex and that it's mostly just you have to hear people talk about it and try working with it and its predecessors before you really get it and understand its benefits.

q64ceo
Jul 7, 2013, 06:39 AM
What you're describing sounds more like an instance method than a class method.

Class methods do one of two things:
1 - Generate a new instance (IE: alloc, stringWithFormat:, ...)
2 - Serve as a stateless utility method that would probably be better off as a plain old C function.

Instance methods are stately. Mostly you want to get and or set things based on the state of an instance of an object.

Is that clear or is it too abstract?

I feel like OOP is fairly complex and that it's mostly just you have to hear people talk about it and try working with it and its predecessors before you really get it and understand its benefits.

I will eventually get it.

Maybe I should spend my time with Xojo. I have always been a BASIC kind of person. And, although its not Objective-C, it is an OOP programming environment based upon BASIC.

Starfia
Jul 7, 2013, 04:28 PM
It sounds like you're just describing methods at this point; not specifically class methods.

In the message [cat feeding], the message "feeding" is sent to an instance of an object "cat." That means the "cat" object that received the message would execute its "feeding" method.

One real benefit of Objective-C objects, though, is that they have methods that can seem intuitively similar to the things they represent. [cat meow] and [cat scratch: (other object) pieceOfFurniture] might be more appropriate methods to implement.

A "class" is a basic definition of a kind of object -- something that could be used to create discrete instances of "cat," for example -- and its instances run methods. Classes can have their own methods too, but -- as someone was saying -- their uses are more generic and limited. If you're not sure, there's no need to use them.

I think the two best resources for this are Apple's document "Programming with Objective-C" and, maybe even better, Stanford's introductory iOS App Development course on iTunes U.