Search code examples
iosnstimeinterval

iOS error "Initializing 'NSTimeInterval *' (aka 'double *')


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;

Solution

  • 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.