views:

67

answers:

3

Hey i am trying to compare the date on which the user opens the app to the date it is currently. (basically, how long they've had the app in days)

Here is the code:

- (NSInteger) daysAfterDate: (NSDate *) aDate
{
    NSTimeInterval ti = [self timeIntervalSinceDate:aDate]; //#1
    return (NSInteger) (ti / D_DAY); //#2
}  //#3

-(void)load {

        NSDate *birthdate = [prefs objectForKey:@"Birthdate"];

    rock_Age =  daysAfterDate(birthdate);

}

errors:

1.) it tells me incompatible types in initialization 2.) D_DAY Undecared

warning:

3.) control reaches end of non void function

If i did this completely wrong, (because for the life of me i cannot understand the NSDate class :/) I would gladly take an alternative to doing this :)

all help is appreciated, Thank you -Jackson Smith

+1  A: 

Does the local method timeIntervalSinceData return an "NSTimeInterval" (typdef double)? I'm guessing it doesn't, hence the error - but the code isn't here to see.

We need to see a bit more code to help you - but the D_DAY undeclared should be easy to resolve. Assuming it's not a #define somewhere in your headers, you need to specify what it is in that function or higher up in the file. I'm guessing you're just missing a #define somewhere that puts in a specific value - at least by syntax.

The warning is because of this error - the parser doesn't know how to complete things nicely until you've fixed that.

heckj
A: 

hi, for 1) i think it talks about "self" ;it is a date?

Dingua
+1  A: 

The load method is correct, the following should work for -[daysAfterDate:].

#define D_DAY        86400

-(NSInteger)daysAfterDate:(NSDate *)aDate {
    NSTimeInterval ti = [[NSDate date] timeIntervalSinceDate:aDate];
    return (NSInteger) (ti / D_DAY);
}

1) is because self is probably no NSDate. Use [NSDate date] to get the current time/date. 2) is because you have to define D_DAY 3) only happens because of 2).

I hope this helps.

The following article might also be informative: (Ars Technica)

mrueg
Alright that solved all of the errors, but made a new one at the line: rock_Age = daysAfterDate(birthdate);
Custard
it says: implicit declaration of function daysAfterDate
Custard
Sorry, my mistake. Either change "rock_Age = daysAfterDate(birthdate);" to "rock_Age = [daysAfterDate:birthdate];" or replace "-(NSInteger)daysAfterDate:(NSDate *)aDate" with "NSInteger daysAfterDate(NSDate *aDate)".
mrueg
hmm alright i switched "rock_Age = daysAfterDate(birthdate);" to "rock_Age = [daysAfterDate:birthdate];" but now it says: "daysAfterDate undeclared" on that line??
Custard
Sorry, it has to be [self daysAfterDate:birthdate]; (It was late yesterday.)
mrueg
Alright, i think that should work :) thanks, you were very helpful!
Custard