0

I'm trying to write a code where i find out if a year is bissextile but on my first line an error appears: "No Know Class method for selector 'datetoday'"... I Don't know why i'm having this error, cause when i tried to put my code into a button it worked just fine... but i need the code to be on 'viewDidLoad', but there it get's that error...

my code:

- (void)viewDidLoad {

[super viewDidLoad];

NSDate *datetoday = [NSDate datetoday];
NSDateFormatter *formatyears = [[NSDateFormatter alloc] init];
[formatyears setDateFormat:@"YYYY"];

NSString *yearStr = [formatyears stringFromDate:datetoday];
int yearint = [yearStr intValue];
int resto;
resto = yearint % 4;

if (resto == 0) {
    teste.text = @"bissexto";
} else {
    teste.text = @"not bissexto";
}
}
JOM
  • 8,139
  • 6
  • 78
  • 111
  • 4
    Shouldn't it be `[NSDate date]`? That returns the current date. –  Jan 07 '13 at 17:30
  • The answer from Anoop is correct for getting the current date and time. However I'd like to say that your calculation of a leap year is over simplistic. I don't know if you already know this but check this SO question: http://stackoverflow.com/questions/725098/leap-year-calculation. – Fábio Oliveira Jan 07 '13 at 17:39

1 Answers1

3

use [NSDate date] to get the current date instead of [NSDate datetoday].

Your code :

NSDate *datetoday = [NSDate datetoday];

Correct code :

NSDate *datetoday = [NSDate date];

And for leap year : ( as understood by Fabio's comment, as your code in non-English, I didnt understood most of the variable names)

if(((yearint %4==0)&&(yearint %100!=0))||(yearint %400==0)){
   NSLog(@"Leap Year");
}
else{
  NSLog(@"Not a Leap Year");
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • 2
    You'd be better off using NSCalendar to find if the year is a leap year or not. – Fogmeister Jan 08 '13 at 13:20
  • I just want to point out that not every calendar supported by iOS/Mac OS X knows leap years. Hebrew Calendar for example knows a leap month. – vikingosegundo Jan 08 '13 at 13:33
  • @vikingosegundo and fogmeister : Then what should i do, the OP wanted to find leap year, although he didnt had problem in that, He had some other mistake, I myself corrected the code to find leap year. – Anoop Vaidya Jan 08 '13 at 13:36
  • Sure, you gave the appropriate answer. We just wanted to point out that calendarial calculations might be not as trivial as expected. – vikingosegundo Jan 08 '13 at 13:38