1

I'm a c++ & c programmer , and i'm new to the world of objective-C , so i have some problem understanding how it works , here a short code , that confused me,

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
    @autoreleasepool {
NSDate *now = [NSDate date];
NSLog(@"The date is %@", now);
double seconds = [now timeIntervalSince1970];
NSLog(@"It has been %f seconds since the start of 1970.", seconds);
}
return 0; }

now is pointer to an object type NSdate my question is why we can not do this :

   double seconds = [NSDate timeIntervalSince1970];

normally the first part is the type of the object and the second part is the method i'm sorry if this is a bad question but i want to understand Objective-C very well from the begining. Thanks

satyres
  • 377
  • 1
  • 5
  • 17
  • 1
    If you know C++ then this is no different from calling `Class::classMethod()` and `myClass.instanceMethod()`. Before learning the language by looking at code, read this: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html – trojanfoe Sep 17 '13 at 16:25
  • ok , but why we can do this NSDate *now = [NSDate date]; ??? – satyres Sep 17 '13 at 16:27
  • because the `-timeIntervalSince1970` is an _instance_ method not a _class_ method. if it'd been a class method, you would have done what you'd like. – holex Sep 17 '13 at 16:28
  • @satyres, because the `+date` is a _class_ method not an _instance_ method. that is why. – holex Sep 17 '13 at 16:29
  • Please guys , i'm sure this a bad question, just wanna understand ,normally when we create a new instance of an object we write this : NSDate *now = [NSDate]; and date is a message for the obejct to know the current date right ? – satyres Sep 17 '13 at 16:32
  • No. `date` is a message you send to the `NSDate` class and it returns an `NSDate` object (an instance in our vocabulary) that represents that particular time point. – Abizern Sep 17 '13 at 16:46
  • MyObjet=[[NSObject alloc] init] , in this case we say that alloc is a class method because it's send to an object (not an instance) and after the creation the init is sent to instance of NSObjcet which is MyObject it self , is this right ? thanks – satyres Sep 18 '13 at 07:46

2 Answers2

0

You can do something similar with

[NSDate timeIntervalSinceReferenceDate];

Although the reference date in this case is 1 January 2001

But, this is a class method. You can call it on the class.

The other methods, such as timeIntervalSince1970 are instance methods, which need to be called on actual objects of the class. In NSDate's case there is no class method for the time interval since 1970.

If you really want to, you can add a Category on to NSDate and add a class method that does this.

Abizern
  • 146,289
  • 39
  • 203
  • 257
0

This is a class method:

NSDate *now = [NSDate date];

You don't need an instance of the object.

This is an instance method:

[now timeIntervalSince1970];

And you need and instance of the object.

Same in C++ as: Class::classMethod() and myClass::instanceMethod()

More info here:

https://softwareengineering.stackexchange.com/questions/191856/what-is-a-static-method-compared-to-instance-class-private-public-methods

Community
  • 1
  • 1
Antonio MG
  • 20,382
  • 3
  • 43
  • 62