一点背景: 我正在自学如何编写 iOS 程序。所以我是初学者。 我只是无法解决这个问题。
我有一个方法,基本上应该在按钮上获取一个变量,将该变量传递给我的变量转换方法。
变量转换方法只是应该通过预定义的 NSDictionary
并基本上搜索变量并返回关联值。
然后使用相关变量更新显示。
即如果 x 的值为 2,那么当我按下 x 时,屏幕应该只显示 2。 我只是想自学 NSDictionary 等等,所以任何帮助都会很棒。
- (IBAction)variablePressed:(id)sender {
NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:2], @"x",
[NSNumber numberWithInt:4], @"y",
[NSNumber numberWithInt:6], @"z", nil];
double newValue = [self.brain convertVariable:[sender currentTitle] usingVariableValues:values];
// NSLog([NSString stringWithFormat:@"%d", newValue]);
}
代码在 NSLog
行崩溃。我把它注释掉了,因为我无法让它工作。
最佳答案
尝试将 NSLog 语句更改为以下...
NSLog(@"newValue is %f", newValue);
NSLog 执行参数替换,因此不需要 stringWithFormat。
关于objective-c - 在编写使用 NSDictionary 将变量转换为数字的方法时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10052530/