2013-05-25 19 views
1

好吧,所以基本上我试图将标签链接到XCode 4.6.2中的一段代码。我使用设计器将其链接起来,但无论我放在哪里,它都会给我这个错误信息。我是xcode的新手,觉得这应该是一个简单的修复。感谢您的反馈/程序中意外的“@”

(void)updateLabel { 
    @property (weak, nonatomic) IBOutlet UILabel *Timer; 

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; 
    NSDateComponents *components = [calendar 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']]; 


    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 


     destinationDate = [[NSDate dateWithTimeIntervalSince1970:1383652800] retain]; 
     timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; 



} 
+1

错误消息是什么???? –

+1

(但在方法定义中不允许使用“@ property”语句,实际上甚至不应该在.m文件中。) –

+0

(实例/属性名称应以小写字母开头。) –

回答

0

@property声明不属于您的函数。 您应该在函数之前始终放置“@”声明。

6

问题是@property只能出现在@interface的内部。这可以在.h文件中或在.m文件中的类扩展中。但它绝对不能放在方法实现中。

鉴于您的财产也是IBOutlet,它应该在.h文件中。

边注:

您创建标签文本的方式很奇怪。至少,这样做:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; 
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0]; 
dateLabel.text = [NSString stringWithFormat:@"%dM %dD %dH %dM %dS", [components month], [components day], [components hour], [components minute], [components second]]; 
更好

的是,使用一个NSDateFormatter

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"M'M' d'D' H'H' m'M' s'S'"]; 
dateLabel.text = [formatter stringFromDate:[NSDate date]]; 
+0

'鉴于你的财产也是一个IBOutlet,它应该在.h文件中:我总是想问是否把'IBOutlets'放在'.h'或'.m'文件中。这有什么理由吗?如果我不需要他们,我总是把它们放在'.m'中。这是不好的风格,甚至是错误的? – HAS

+1

@HAS您的.h为您的课程提供了公共接口。如果您将Interface Builder作为您班级的另一个客户端,那么在.h中添加'IBOutlet'是最有意义的。根本不需要在.m文件中使用IBOutlet,因为IB不需要了解您的实现。 – rmaddy

+0

这是一个好点,对我有意义,非常感谢! – HAS