好吧,基本上我正在尝试将标签链接到 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];
}
最佳答案
问题是 @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]];
关于objective-c - 程序中出现意外的 "@",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16753355/