objective-c - 日期计算中的语法错误

标签 objective-c ios xcode

我在 Xcode 中使用以下代码:

(void)updateLabel 
{
    NSDate* now = [NSDate date];
    int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
    int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
    int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
}  

我收到以下错误。我该如何解决这个问题?

error: invalid operands to binary - (have 'int' and 'id')

最佳答案

你一定是这样使用的:-

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
[gregorian release];

希望对你有帮助

关于objective-c - 日期计算中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14214378/

相关文章:

iphone - 如何在代码中而不是 Interface Builder 中设置 didEndOnExit?

iphone - UIPickerView 设置选中

objective-c - 使用 NSXMLParser 解析问题

ios - 将 www 文件夹重新添加到 xcode phonegap 项目

iphone - 为什么 iphone SDK NSTimer 没有调用我告诉它的函数?

ios - 如何从扩展/嵌套的 UITableViewCell 导航到 View Controller

ios - NSUserDefault 中的 NSMutableArray,检索 NULL

iphone - 将 NSTimer 设置为在将来触发一次

iphone - 在 appDelegate.m 中设置导航 Controller 和 tabBarController

ios - 类型 'AppDelegate' 的值没有成员 'window'