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

7
  • 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: developer.apple.com/library/mac/documentation/Cocoa/Conceptual/… Commented Sep 17, 2013 at 16:25
  • ok , but why we can do this NSDate *now = [NSDate date]; ??? Commented Sep 17, 2013 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. Commented Sep 17, 2013 at 16:28
  • @satyres, because the +date is a class method not an instance method. that is why. Commented Sep 17, 2013 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 ? Commented Sep 17, 2013 at 16:32

2 Answers 2

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.

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

Comments

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

2 Comments

Are they called "static" in Objective-C?
Nope. To avoid just this kind of confusion, the terms are Class Methods and Instance Methods. To be pedantic, there are no static methods in Objective-C

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.