2013-05-25 17 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