[英]xcode objective c, EXC_BAD_ACCESS
I have this property in my .h file: 我的.h文件中有此属性:
@property (nonatomic, readwrite) NSInteger gameMode;
And I have this setter in the corresponding .m file: 我在相应的.m文件中有此设置器:
- (void)setGameMode:(NSInteger)mode {
self.gameMode = mode;
NSLog(@"game mode %d", self.gameMode);
}
In my view controller, I have this function to call the setter when I touch a button: 在我的视图控制器中,当我触摸按钮时,我可以使用以下功能来调用设置器:
- (IBAction)touchCardButton:(UIButton *)sender {
if ([self.game disableModeChooseAtFirstTouch]) {
[self.game setGameMode:self.modeSegmentedControl.selectedSegmentIndex];
}
But when I click the button in simulator, it will stop here: 但是,当我单击模拟器中的按钮时,它将在此处停止:
self.gameMode = mode;
And says: 并说:
Thread 1: EXC_BAD_ACCESS
线程1:EXC_BAD_ACCESS
Update: If I add a NSLog before it, like this: 更新:如果我在其之前添加一个NSLog,如下所示:
- (void)setGameMode:(NSInteger)mode {
NSLog(@"game mode %d", self.gameMode);
self.gameMode = mode;
NSLog(@"game mode %d", self.gameMode);
}
It will keep printing game mode: 0
它将继续打印
game mode: 0
Can anybody tell me why? 谁能告诉我为什么? Thanks!
谢谢!
Yes, I can tell you why. 是的,我可以告诉你原因。 You are invoking the setter in the setter, causing infinite recursion, stack overflow, and a crash.
您正在setter中调用setter,从而导致无限递归,堆栈溢出和崩溃。
Your setGameMode method should read: 您的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.