1

I know usually, when you want to call a method on another object, you do:

NewObject *object = [NewObject alloc]init];

[object callMethod];

But I created a class that isn't an object itself meaning it doesn't have properties or memory management. It has a couple methods that calculate some stuff.

From any other class, all I have to do is import the header for this class and do:

#import "MyClass.h"

[MyClass callMethod];

Why in this case do I not have to alloc init? It works just fine.

1
  • they are called Class methods so you do not need to instantiate an object to call them.You need to init instances of a class. Commented Aug 9, 2012 at 21:23

4 Answers 4

1

It sounds like you are trying to call a class method. These are methods which have been defined as:

  • +(void) myStaticMethod;

instead of

  • -(void) myMethod;

The plus sign indicates that the method does not use any fields, and thereby does not need to instantiate the object.

In your example, "object" is an instance of a class "NewObject" which has been allocated memory and initialized. Where-as your example, "MyClass" is only a class which because it has static members declared as above, does not need to be instantiated.

Class methods provide a nice way to combine a bunch of related functions into one place, rather than having them spread out in the regular namespace, as would usually be done in straight C. You can also have both class methods and instance methods in the same class, using the class ones when needed, and instantiating the class to use the instance ones when needed.

EDIT: Changed terminology to refer to class methods instead of static methods.

Sign up to request clarification or add additional context in comments.

2 Comments

static and class methods are absolutely different things. ObjC does not have static methods.
Max, thanks for setting me straight. I always referred to them as static methods as that is what C++ calls them, but I was using the wrong terminology for Objective C. I have edited the response to correct.
1

because you are calling a class method. You only need to alloc init objects. Classes only need to be included but not alloc inited. So you don't need to init an NSString class, say.

Edit:

Let's just have some nonsense examples:

+ (void)classMethod {
    NSLog("Hi!");
}

[SomeClass classMethod];     // prints Hi!

- (void)instanceMethod {     // (say it's an instance method of NSString)
    NSLog(self);
}

[@"someNSString" instanceMethod];   // prints someNSString. But you need to have a string first, otherwise you cannot use this method.

3 Comments

The method I have is a class method, but it doesn't return anything. What is the difference between a class and instance method when both don't return anything?
The difference is whether you apply that method to a class or to an object. For example, if you want to call stringByAppendingString, you want to call it on an existing NSString object. You cannot append a string to a class! On the other hand, if you want to get the current time, you don't need to have an object to get it. You can just call the NSDate class for that.
Oops sorry I should have given examples for not returning anything. Let's see. Lemme edit my answer.
0

There is a difference between "instance" methods (normal ones), that have to be called on an object and have access to self, and "class" methods (called static, in many programming languages), that are invoked on the class and thus do not have a self.

2 Comments

Oh ok. So in my class, the class methods don't return anything. Would I able to accomplish the same thing by making this class a singleton and then calling the method on that singleton from throughout the app? Which is the better approach?
Class functions are a better choice than a singleton in that case. A singleton is a good choice if you have some variables that have to be shared between the various members; if you have just a lone method, use a class function.
0

Class methods are similar to C++ static methods, in that they can be invoked without creating a concrete instance of the class. The usefulness of this is you can call a class's specialized factory methods to create a new instance; or, you can define a utility library under the scope of a class that may or may not provide concrete instances depending on the task.

Look at NSDate and NSNumber are good examples of this in the Foundation framework.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.