我在 Objective-c 中有以下代码片段,它会自动将一些变量设置为多个预设值。但是,在标记的行上,我收到“程序接收信号:EXC_BAD_ACCESS”异常。
让我感到困惑的是,上面的行是相同的,只是值不同。如果第 1 行没有抛出异常,为什么第 2 行应该抛出异常?我能做些什么呢?
提前致谢!
斯特伦
- (IBAction)presetPressed:(id)sender {
if(self.userEnteringNumber) [self enterPressed];
double xVal;
double aVal;
double bVal;
NSString *preset = [sender currentTitle];
if ([preset isEqualToString:@"1"]) {
xVal = 1;
aVal = 2;
bVal = 3;
} else if ([preset isEqualToString:@"2"]) {
xVal = 1.5;
aVal = 2.9;
bVal = 3.0;
} else if ([preset isEqualToString:@"3"]) {
xVal = -1;
aVal = -2;
bVal = -3;
}
[self.variables setValue:[NSNumber numberWithDouble:xVal] forKey:@"x"];
[self.variables setValue:[NSNumber numberWithDouble:aVal] forKey:@"a"];
[self.variables setValue:[NSNumber numberWithDouble:bVal] forKey:@"b"];
self.xVar.text = [NSString stringWithFormat:@"= %@", xVal];
self.aVar.text = [NSString stringWithFormat:@"= %@", aVal]; //EXC_BAD_ACCESS here
self.bVar.text = [NSString stringWithFormat:@"= %@", bVal];
[self calculateResult];
}