0

我得到这两个错误: - 不能使用对象作为方法的参数 - 作为回报的类型不兼容

那是我的代码:

 - (NSString) dateStringFromUnixTimeStamp:(NSInteger)timeStamp {
 //Create Date-String from UNIX-Time-Stamp:
 NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
 NSDateComponents *monthComponents = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit fromDate:date];
 int month = [monthComponents month];

 NSDateComponents *dayComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:date];
 int day = [dayComponents day] - 1;

 NSDateComponents *yearComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:date];
 int year = [yearComponents year];

 NSString *monthString;

 switch (month) {
  case 1:
   monthString = @"Januar";
   break;
  case 2:
   monthString = @"Februar";
   break;
  case 3:
   monthString = @"März";
   break;
  case 4:
   monthString = @"April";
   break;
  case 5:
   monthString = @"Mai";
   break;
  case 6:
   monthString = @"Juni";
   break;
  case 7:
   monthString = @"Juli";
   break;
  case 8:
   monthString = @"August";
   break;
  case 9:
   monthString = @"September";
   break;
  case 10:
   monthString = @"Oktober";
   break;
  case 11:
   monthString = @"November";
   break;
  case 12:
   monthString = @"Dezember";
   break;
  default:
   break;
 }

 NSString *dateString = [[NSString stringWithFormat:@"%d", day] stringByAppendingString:@". "];
 dateString = [dateString stringByAppendingString:monthString];
 dateString = [dateString stringByAppendingString:@" "];
 dateString = [dateString stringByAppendingString:[NSString stringWithFormat:@"%d", year]];

 return dateString;
}

感谢帮助!

4

1 回答 1

2

您的方法的返回类型必须是NSString *而不是NSString.

也就是说,您的方法比它必须的要复杂得多。您应该使用 anNSDateFormatter来格式化日期。将此方法变成几行。

此外,如果您报告错误消息,您应该始终告诉我们错误发生在哪一行。

于 2010-06-26T11:53:05.523 回答