[英]xcode objective c, EXC_BAD_ACCESS
我的.h文件中有此屬性:
@property (nonatomic, readwrite) NSInteger gameMode;
我在相應的.m文件中有此設置器:
- (void)setGameMode:(NSInteger)mode {
self.gameMode = mode;
NSLog(@"game mode %d", self.gameMode);
}
在我的視圖控制器中,當我觸摸按鈕時,我可以使用以下功能來調用設置器:
- (IBAction)touchCardButton:(UIButton *)sender {
if ([self.game disableModeChooseAtFirstTouch]) {
[self.game setGameMode:self.modeSegmentedControl.selectedSegmentIndex];
}
但是,當我單擊模擬器中的按鈕時,它將在此處停止:
self.gameMode = mode;
並說:
線程1:EXC_BAD_ACCESS
更新:如果我在其之前添加一個NSLog,如下所示:
- (void)setGameMode:(NSInteger)mode {
NSLog(@"game mode %d", self.gameMode);
self.gameMode = mode;
NSLog(@"game mode %d", self.gameMode);
}
它將繼續打印game mode: 0
誰能告訴我為什么? 謝謝!
是的,我可以告訴你原因。 您正在setter中調用setter,從而導致無限遞歸,堆棧溢出和崩潰。
您的setGameMode方法應顯示為:
- (void)setGameMode:(NSInteger)mode
{
_gameMode = mode; //Don't use the setter in the implementation of the setter.
NSLog(@"game mode %d", self.gameMode);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.