문제

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;
도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top