Frage

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;
War es hilfreich?

Lösung

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.

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top