Problem with Function to convert UNIX TimeStamp
I get those two errors: - Can not use an object as parameter to a method - Incompatible types in return
That's my code:
- (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:
开发者_如何学Python 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;
}
Thanks for help!
The return type of your method must be NSString *
instead of NSString
.
That said, your method is much more complex than it has to be. You should use an NSDateFormatter
to format a date. Turns this method into a handful of lines.
Also, if you report error messages, you should always tell us on what line the error occurs.
精彩评论