-1

For this line of code:

NSHost *now = [NSHost currentHost];

Why is it that the method, currentHost, returns a pointer to NSHost and not to currentHost?

alexswo
  • 539
  • 1
  • 4
  • 3

3 Answers3

0

It is a class method:

Returns an NSHost object representing the host the process is running on.

That is what the method was designed it do.

zaph
  • 111,848
  • 21
  • 189
  • 228
0

The method does not return a pointer to NSHost, what is a type and class object, but to an instance object of the class NSHost. This instance object is the current host.

(int * is not a pointer to int, but a pointer to a C object of the type int.)

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
-1

NSHost is a class name and currentHost is a static method of that class. So all objects of type NSHost share one currentHost. For example you can check the self value gainst NSHost to see if your object is the currentHost or something else.

ikrabbe
  • 1,909
  • 12
  • 25
  • Actually a class method, not a static method. – zaph Jun 26 '15 at 21:33
  • So would this be similar to how polymorphism is implemented? – alexswo Jun 26 '15 at 21:35
  • @zaph thats largely a synonym. I can't tell the difference, but it seems that the idea of class methods is nearer to the idea of interfaces, while static methods are more a part of a class definition. – ikrabbe Jun 26 '15 at 21:40
  • In Objective-C classes really do have class methods. One class method can call another class method of the same class with self. They must be called with a reference to the class, not an instance of the class. See this [SO answer](http://stackoverflow.com/a/1053646/451475) and this [Apple Documentation](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/DefiningClasses/DefiningClasses.html#//apple_ref/doc/uid/TP40011210-CH3-SW18) – zaph Jun 26 '15 at 21:46
  • @zaph an instance method has the same distance to a class method as to a static method. It's just another name. But for objective-c the name class method might be preferred. I don't really care. – ikrabbe Jun 26 '15 at 21:56
  • Class methods are part of the class, they support inheritance, can and are subclassed. Look at NSObject class methods, these methods are commonaly subclassed. (Oh, I'm not the down vote). – zaph Jun 26 '15 at 22:20