objective-c - 在编写使用 NSDictionary 将变量转换为数字的方法时遇到问题

标签 objective-c ios nsdictionary

一点背景: 我正在自学如何编写 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/

相关文章:

ios - 应用程序在 gpus_ReturnNotPermittedKillClient 崩溃

iphone - 防止标签栏更改特定索引处的标签 - IOS

objective-c - NSString 中的 stringWithString 和 initWithString 中的对象所有权

iphone - 如何使用 iPhone SDK 从 SQLite 数据库中获取原始特殊字符?

ios - NSComparator block 未调用

objective-c - 将文件树结构转换为 NSDictionary

ios - 如何检查值 null 并在 NSDictionary 中替换它

objective-c - 如何从 iOS 中的 xml 中提取文档流?

ios - 打开ios拨号面板

ios - 创建 NSManagedObject 的副本