0

It's just something which is not logical for me. Sure it's useful to call methods within in a class by the self-keyword. But why isn't it possible calling it by the own classname??

e.g. [MyClassWhereIAmActuallyIn anyRandomMethod]; instead of [self anyRandomMethod];

Wain
  • 118,658
  • 15
  • 128
  • 151
Michael
  • 1,030
  • 14
  • 29

3 Answers3

2

Because that has a different meaning.

[self someMethod]

sends someMethod to the object, whose reference is stored in the (slightly magic) variable self.

[SomeClass someMethod]

sends someMethod to the class object (yes, classes are objects, too), which contains the meta-information for class SomeClass.

Two different objects ("receivers"). Also note, that there are class methods in Objective-C (i.e., you can take advantage of the fact, that classes are objects, and define new methods for them). Observe:

@interface SomeClass
    - (void) someMethod;
    + (void) someMethod;
@end

These are completely different methods, intended for completely different receivers. The method tagged with - is an instance method (will be used, e.g., with self). The method tagged with + is a class method (and will be used with the class object).

Dirk
  • 30,623
  • 8
  • 82
  • 102
  • ok, that makes sense. But when I'm calling my own class methods... wouldn't it be logical to be able to call it by the class name as well?? – Michael Mar 31 '14 at 15:36
  • Not sure, whether I understand the question... What do you mean by "class method" here? There is nothing special about class methods (the `+` guys). A method call is always `[ message...]` in Objective-C. And a class is a perfectly fine receiver. But I suspect, that we might mean different things by "class method". – Dirk Mar 31 '14 at 16:00
  • CLASS-A h-file i declared a method: `+(void)greet;` CLASS-A m-file `+(void)greet {NSLog(@"hi")};` CLASS-B m-file call method: `[CLASS-A greet];` CLASS-A m-file call method: `[CLASS-A greet];` // why not `[self greet];` ?? I thought that I can just call the own Class-Method by calling it with `[self greet]` but now I guess I can't use it because "self" represents just my Class-Objects and not the Class iself?? – Michael Apr 01 '14 at 09:30
  • I still cannot really understand your question... Your "Class-Objects" are your "Class[es]". Do you mean "instances"? You cannot call a class method (`+`) in an instance method (`-`, even if it's an instance method of the same class) using `self` as the receiver, since `self` in the instance method refers to the instance. The class is a totally different object. – Dirk Apr 01 '14 at 11:22
1

You can call class method ('+') like that. Self is a pointer to an instance of your class in instance methods ('-'), self points to the singleton Class-object when you are in class methods ('+').

Joride
  • 3,722
  • 18
  • 22
0

In OOP this is the difference between the class object and an instance of that class object. When you create a method, you specify whether it is a class method (+) or an instance method (-). Once the method is defined you need to call it in the appropriate way (on the class object or on an instance of that class).

Wain
  • 118,658
  • 15
  • 128
  • 151