Class method vs instance method

Dear All,

I got a question for you. I am developing an ASOC app and I’ve got some ObjC classes too (with methods). I know that I can call both ObjC instance or class methods from my script using this syntax:


property aClass: class "ClassObjC"

instance method:


set instanceOfClass to aClass's alloc()'s init()
instanceOfClass's method()

class method:


aClass's method()

My question is, how do I choose what kind of method I should create in my ObjC classes, a class or instance one? I converted all the instance methods I had in my classes to class methods so they were easier to be called from my script without allocating any memory myself, but am I doing this right? Is there anything I should consider before converting a instance method to a class method? The documentation on the usage and difference between these two type of methods is not really clear to me. Can you help?

Thank you in advance.

Pier

If they’re standalone methods, it really doesn’t matter. It’s when you want the methods to store state that you require instance methods.

Dear Shane, thanks for your answer! Could you give me some more detail? what do you mean by storing state?

As in relying on properties to hold values between calls.

By definition you cannot turn instance methods into class methods. If the method is an instance or class method is not defined by how they are called, it’s defined by how they work. A class method will give the same results when it’s called from any instance or class while instance methods are not and cannot be called from the class itself. That’s how a class method and instance method are different from each other. So if you’re able to call the method directly from the class then it was an class method all the time, you just called it wrong.

Thanks both for your answers, but I do have some final questions.

Keeping your explanation in mind, why, in many Objc projects, some classes are initialised as objects before calling their methods (as instance methods)? Why would you create a instance of a class if its methods will always return the same results anyway? I did notice that sometimes there is not even an init method in the class. Nevertheless, before calling methods from the class, they do create an instance of the class and then they call all methods as instance methods. This is probably the core of my misunderstanding.

Thank you.

I presume you’re thinking of classes like NSFileManager with -defaultManager. This is what’s known as a singleton pattern – look it up on Google for a full explanation.

(And all classes do have an init method.)

Because Objective-C lacks constructor and destructor methods like C++ have for instance. Therefore an explicit constructor method has to be called after an object is allocated, mostly named initializers in Objective-C.

Singletons for instance. Singletons can be very useful, they behave like classes but it can make use of properties of that single instance. NSWorkSpace is a singleton and NSFileManager can be used both as an instance or singleton. You can recognize these classes by their class method names starting with shared. or default. respectively.

When the class doesn’t need any construction, the class is a singleton or the explicit constructor method is not named init.

edit: p.s. FWIW, I didn’t saw Shane’s post before I replied.