倒计时应用程序给出预期的“]”错误

时间:2011-12-15 09:50:02

标签: iphone objective-c ios ios5

我正在为iPhone和iPad构建我的第一个应用程序,它是一个简单的应用程序,可以计算到给定的月,日,小时,分钟和秒。

修复了几个错误后,会出现以下错误:

  

预期“]”

以下是代码:

#import "ViewController.h"
@implementation ViewController
-(void)updateLabel {
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *components = [calender components:units fromDate:[NSDate date] toDate:destinationDate options:0 ];
    [dateLabel setText:[[NSString stringWithFormat:@"%d%c  %d%c  %d%c  %d%c  %d%c" , [components month], 'm' [components day], 'd' , [components hour], 'h' , [components minute], 'm' , [components second], 's' ]]; 
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    destinationDate = [[NSDate dateWithTimeIntervalSince1970:1325153549] retain];
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector (updateLabel) userInfo:nil repeats:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}
@end

4 个答案:

答案 0 :(得分:2)

    [dateLabel setText:[[NSString stringWithFormat:@"%d%c  %d%c  %d%c  %d%c  %d%c" , [components month], 'm' [components day], 'd' , [components hour], 'h' , [components minute], 'm' , [components second], 's' ]]; 

不平衡的括号?

答案 1 :(得分:1)

我想是这个吗?

[dateLabel setText:[[NSString stringWithFormat:@"%d%c  %d%c  %d%c  %d%c  %d%c" , [components month], 'm' [components day], 'd' , [components hour], 'h' , [components minute], 'm' , [components second], 's' ]]; 

你打开三个[在NSString之前的第一部分,但最后只关闭其中两个。这看起来像你曾经[太多开始,实际上想要

[dateLabel setText:[NSString stringWithFormat:@"%d%c  %d%c  %d%c  %d%c  %d%c" , [components month], 'm' [components day], 'd' , [components hour], 'h' , [components minute], 'm' , [components second], 's' ]]; 

答案 2 :(得分:0)

此行包含语法错误:

[dateLabel setText:[NSString stringWithFormat:@"%d%@  %d%@  %d%@  %d%@  %d%@" , [components month], @"m", [components day], @"d" , [components hour], @"h" , [components minute], @"m" , [components second], @"s"]]; 

你错过了一个逗号并且在NSString之前有太多的左括号。

答案 3 :(得分:0)

错误究竟在哪里?我想可能就在这里:

[dateLabel setText:[[NSString stringWithFormat:@"%d%c  %d%c  %d%c  %d%c  %d%c" , [components month], 'm' [components day], 'd' , [components hour], 'h' , [components minute], 'm' , [components second], 's' ]]; 

好像你忘了这里的逗号

'm' [components day]

希望有所帮助

修改

在NSString

之前有1“[”太多了