Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 607
rattletrap99 Avatar asked Feb 05 '14 17:02

rattletrap99


2 Answers

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.

like image 180
Gavin Avatar answered Oct 06 '22 08:10

Gavin


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.

like image 25
phi Avatar answered Oct 06 '22 07:10

phi