gpt4 book ai didi

ios - 如何形成一个 if 语句来选择几个字典中的一个?

转载 作者:行者123 更新时间:2023-11-28 22:01:15 26 4
gpt4 key购买 nike

我需要将关键值代入方程式。这些值需要根据分段控件的位置来自一个或另一个字典。

这是分段控件的代码:

- (IBAction)tankChooser:(UISegmentedControl *)sender
{
if (_tankControl.selectedSegmentIndex ==0)
{
bigTank = YES;
tankType = @"dragon500";
}
if (_tankControl.selectedSegmentIndex ==1)
{
bigTank = NO;
tankType = @"skid250";
}
if (_tankControl.selectedSegmentIndex ==1)
{
bigTank = YES;
tankType = @"hyTech500";
}
}

这就是我一直在使用的单一词典,而且效果很好。

- (void)calculateRate
{
NSString *dragonPath = [[NSBundle mainBundle] pathForResource:@"dragonBarrelChart" ofType:@"plist"];
NSDictionary *dragonBarrelChart = [NSDictionary dictionaryWithContentsOfFile: dragonPath];

NSString *startKey = [NSString stringWithFormat:@"%@%@", self.startLevel.text, startFractions];

NSString *startValue = dragonBarrelChart[startKey];

本质上,我需要一种方法,以便当用户在分段控件上选择一个按钮时,它会导致 NSString *startvalue 从不同的字典中获取它的键/值,就像这里的这个:

    NSString *hyTechPath = [[NSBundle mainBundle] pathForResource:@"hyTechBarrelChart" ofType:@"plist"];
NSDictionary *hyTechBarrelChart = [NSDictionary dictionaryWithContentsOfFile: hyTechPath];

如果我能提供更多信息,请告知。在此先感谢您的帮助!

最佳答案

如果我理解正确,您的目标是从与用户选择的坦克类型相对应的 plist 文件中检索“图表”数据。一种方法是将资源的名称保存在一个实例变量中,然后 calculateRate 将使用该实例变量来检索正确的字典。您的 tankChooser 方法看起来像这样。

- (IBAction)tankChooser:(UISegmentedControl *)sender
{
if (_tankControl.selectedSegmentIndex ==0)
{
bigTank = YES;
tankType = @"dragon500";
tankResource = @"dragonBarrelChart";
}
if (_tankControl.selectedSegmentIndex ==1)
{
bigTank = NO;
tankType = @"skid250";
tankResource = @"skidBarrelChart";
}
if (_tankControl.selectedSegmentIndex ==2)//assuming you meant 2 here
{
bigTank = YES;
tankType = @"hyTech500";
tankResource = @"skidBarrelChart";
}
}

然后在费率计算器方法中:

- (void)calculateRate
{
NSString *tankPath = [[NSBundle mainBundle] pathForResource:tankResource ofType:@"plist"];
NSDictionary *tankBarrelChart = [NSDictionary dictionaryWithContentsOfFile: tankPath];
//and then use this to get startValue.
}

关于ios - 如何形成一个 if 语句来选择几个字典中的一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25028110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com