objective-c - 程序中出现意外的 "@"

标签 objective-c symbols

好吧,基本上我正在尝试将标签链接到 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/

相关文章:

c++ - Duplicate symbol --- 定义和声明的区别

c++ - 使用堆栈跟踪检测功能

ios - 如何从雅虎天气预报 XML 文档中获取图像 url?

c++ - 为什么 WinDbg 无法使用 Service Pack 2 x86 零售符号下载从 Windows Server 2003 加载符号?

scala - 在 Scala 中使用符号的实际例子?

c++ - 在 C++ 中执行期间的 "undefined symbol"

ios - 获取内存不足崩溃的通知

ios - 在 Xcode 7 中单击 UITableView 的单元格时,详细信息 VC 的重定向不起作用

iphone - 滚动时 UITableView 崩溃

iphone - NSBlockOperation 和启动方法