我在理解objective-c,並且在點擊屏幕和遞增存儲在我的appdelegate中的count變量時出現問題。在Objective-C中按值傳遞?
- (void)updateLabel:(NSInteger)num {
NSString *s = [[NSString alloc] initWithFormat:@"%@", num];
countLabel.text = s;
[s release];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
TestAppDelegate *aDel = (TestAppDelegate *)[UIApplication sharedApplication].delegate;
aDel.count++;
NSInteger num = aDel.count;
[self updateLabel:num];
}
我得到的EXC_BAD_ACS對我來說,我試圖訪問我不是。它看起來像我不能發送updateLabel num變量,因爲基本類型的範圍消失在方法的末尾,然後當updateLabel試圖訪問它,我得到的錯誤?我想知道我是否正確理解了這個概念。謝謝。
你已經很好地展示了你的想法。但'updateLabel'中的代碼在*'touchedBegan'完成之前運行,所以傳遞給它的任何東西都不會超出範圍。 taskinoor有正確的答案。 – 2011-03-23 07:09:34