5

I'm trying to get the time interval between two NSDates, namely previousActivity.stopTimeand previousActivity.startTime. I'm getting an error with this code:

NSTimeInterval *previousActivityDuration = [previousActivity.stopTime timeIntervalSinceDate:previousActivity.startTime];

Here's the error message:

"Initializing 'NSTimeInterval *' (aka 'double *') with an expression of incompatible type 'NSTimeInterval' (aka 'double')"

I don't get it; If NSTimeInterval is aka 'double', how is the initialization expression incompatible, and how do I fix it?

Many thanks!

Edit:

Per @Rmaddy's comment, I removed the asterisk. Then I get this error in the line immediately following:

Assigning to 'NSNumber *' from incompatible type 'NSTimeInterval' (aka 'double')

Here's the offending line:

previousActivity.duration = previousActivityDuration;
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
rattletrap99
  • 1,469
  • 2
  • 17
  • 36

2 Answers2

11

You're attempting to make an NSTimeInterval pointer variable, but what you really want is just an NSTimeInterval variable. So remove the asterisk like this:

NSTimeInterval previousActivityDuration = [previousActivity.stopTime timeIntervalSinceDate:previousActivity.startTime];

Your code was complaining because the types were different. Usually we only use pointers for Objective-C objects, and only where necessary do we generally use pointers for primitive types such as doubles and such.

Gavin
  • 8,204
  • 3
  • 32
  • 42
4

If you read the error carefully, you can understand that you need to change

NSTimeInterval *previousActivityDuration

to

NSTimeInterval previousActivityDuration

(remove the asterisk)

If you cmd+click NSTimeInterval you will see this line:

typedef double NSTimeInterval;

meaning that it's just a primitive, not a class.

phi
  • 10,634
  • 6
  • 53
  • 88