0

我正在尝试编写一个代码来确定一年是否是 bissextile 但在我的第一行出现错误:“选择器'datetoday'的不知道类方法”......我不知道为什么我有这个错误,因为当我试图将我的代码放入一个按钮时它工作得很好......但我需要代码在'viewDidLoad'上,但它得到了那个错误......

我的代码:

- (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";
}
}
4

1 回答 1

3

用于[NSDate date]获取当前日期而不是 [NSDate datetoday].

你的代码:

NSDate *datetoday = [NSDate datetoday];

正确的代码:

NSDate *datetoday = [NSDate date];

对于闰年:(正如法比奥的评论所理解的,因为你的代码是非英语的,我不理解大多数变量名)

if(((yearint %4==0)&&(yearint %100!=0))||(yearint %400==0)){
   NSLog(@"Leap Year");
}
else{
  NSLog(@"Not a Leap Year");
}
于 2013-01-07T17:31:21.263 回答